Java 8 Features
- Lambda Expression
- Functional interfaces
- Method references
- Stream API
Java Programming Language
- Lambda Expressions, a new language feature, has been introduced in this release. They enable you to treat functionality as a method argument, or code as data. Lambda expressions let you express instances of single-method interfaces (referred to as functional interfaces) more compactly.
- Method references provide easy-to-read lambda expressions for methods that already have a name.
- Default methods enable new functionality to be added to the interfaces of libraries and ensure binary compatibility with code written for older versions of those interfaces.
- Repeating Annotations provide the ability to apply the same annotation type more than once to the same declaration or type use.
- Type Annotations provide the ability to apply an annotation anywhere a type is used, not just on a declaration. Used with a pluggable type system, this feature enables improved type checking of your code.
- Improved type inference.
- Method parameter reflection.
- Collections
Concurrency
- Classes and interfaces have been added to the java.util.concurrent package.
- Methods have been added to the java.util.concurrent.ConcurrentHashMap class to support aggregate operations based on the newly added streams facility and lambda expressions.
- Classes have been added to the java.util.concurrent.atomic package to support scalable updatable variables.
- Methods have been added to the java.util.concurrent.ForkJoinPool class to support a common pool.
- The java.util.concurrent.locks.StampedLock class has been added to provide a capability-based lock with three modes for controlling read/write access.
JDBC
- The JDBC-ODBC Bridge has been removed.
- JDBC 4.2 introduces new features.
java.lang and java.util Packages
- Parallel Array Sorting
- Standard Encoding and Decoding Base64
- Unsigned Arithmetic Support
Inner Class
·
Inner classes let you define one class within
another
·
Inner class is a part of the outer class
·
Inner class instance has access to all members
of the outer class, even those marked private
·
Lambda expressions, which are often used as an
alternative syntax (shorthand) for inner classes
1.
Inner classes
(“nested class” in the objective)
2.
Method-local
inner classes (“local class” in the objective)
3.
Anonymous inner
classeses
4.
Static nested
classes (“static inner class” in the objective)
·
Event handlers are perhaps the best example of inner
class
public class MyClass
extends Applet {
...
someObject.addMouseListener(new
MyAdapter());
...
class MyAdapter extends MouseAdapter {
public void mouseClicked(MouseEvent e)
{
...//Event listener
implementation goes here...
}
}
}
One of the key benefits of an inner class is the
“Special relationship” an inner class instance shares with an instance of the outer class. That “special relationship” gives code in the inner class access to members of the enclosing (outer) class, as if the inner class were part of the outer class
1. Inner classes:
{
class Inner{}
|
Javac Outer.java |
Outer.class |
|
Outer$Inner.class |
|
|
Java Outer.java |
Outer$Inner.java is wrong not going to work as there is no
main method |
public static void main(String args[])
{
Outer.Inner in= new Outer.new Inner();
Inn.innerMethod();
System.out.println(x);
System.out.println(this.x);
System.out.println(Outer.x);
}
Member Modifiers Applied to Inner
Classes
A
regular inner class is a member of the outer class just as instance variables
and methods are, so the following modifiers can be applied to an inner class:
1.
final
2.
abstract
3.
public
4.
private
5.
protected
6.
static—but static turns it into a static nested
class, not an inner class
7.
strictfp
2. Method-local inner classes
{
void outerMethod(){class Inner{}}
What a Method-Local Inner Object Can and Can’t Do
A method-local inner class can be instantiated only within the method where the inner class is defined.
In other words, no other code running in any other method—inside or outside the outer class—can ever instantiate the method-local inner class.
Like regular inner
class objects, the method-local inner class object shares a special
relationship with the enclosing (outer) class object and can access its private
(or any other) members. However, the inner class object cannot use the local
variables of the method the inner class is in. Why not?
Think about it.
The local variables of the method live on the stack and exist only for the lifetime of the method. You already know that the scope of a local variable is limited to the method the variable is declared in. When the method ends, the stack frame is blown away and the variable is history. But even after the method completes, the inner class object created within it might still be alive on the heap if, for example, a reference to it was passed into some other code and then stored in an instance variable. Because the local variables aren’t guaranteed to be alive as long as the method-local inner class object is, the inner class object can’t use them. Unless the local variables are marked final or are ef ectively final! The following code attempts to access a local variable from within a method-local inner class:
Just a reminder about modifiers within a method: The same rules apply to method-local inner classes as to local variable declarations.
You can’t, for example, mark a method-local inner class public, private, protected, static, transient, and the like. For the purpose of the exam, the only modifiers you can apply to a method-local inner class are abstract and final, but, as always, never both at the same time.
Remember that a local class declared in a static method has access to only static members of the enclosing class, since there is no associated instance of the enclosing class. If you’re in a static method, there is no this, so an inner class in a static method is subject to the same restrictions as the static method. In other words, no access to instance variables
Anonymous inner classeses
- In Java: inner classes declared without any class name at all (hence, the word anonymous)
- We can define these classes, not just within a method, but even within an argument to a method
payment()
{
}
}
payment()
{
}
}
{
public static void main(String args)
{
}
}
package normalClass;public class Card {int a=10;public void payment(){System.out.println("Payment inside card");}public void cash(){System.out.println("Payment inside cash pay");}}
No comments:
Post a Comment