Updates in jdk 1.8
By
Sharmilee
9894303344
Java Trainer
Mazenet Solution
Objectives
• What’s new with JAVA -8
• What’s been modified with JAVA – 8
• What’s gone
What’s new with JAVA -8
1. Lambda Expression
2. Method references
3. Functional Interface
4. Default method
5. Optional
6. Parallel sort
7. Calender.Builder
Sorting in Java 8
private void sortUsingJava7(List<String> names) {
Collections.sort(names, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return s1.compareTo(s2);
} }); }
private void sortUsingJava8(List<String> names) {
Collections.sort(names, (s1, s2) -> s1.compareTo(s2)); }
1) Lambda Introduction (new to jdk 1.8)
What is lambda?
• Lambda expression facilitates functional
programming, and simplifies the development
a lot
Syntax of lambda expression
• “ -> ” is known as lambda expression.
parameter -> expression body
Characteristics of Lambda expression
• Optional type declaration
• Optional parenthesis around parameter
• Optional curly braces
• Optional return keyword
2) Method References (new to jdk 1.8)
Method References
• It help to point to methods by their names.
• A method reference is described
using :: (double colon) symbol.
• A method reference can be used to point the
following types of methods −
Static methods
Instance methods
Constructors using new operator (TreeSet::new)
Types of method reference
Type Example Syntax
1. Reference to a static
method ContainingClass::staticMethodName Class::staticMethodName
2. Reference to a constructor ClassName::new ClassName::new
3. Reference to an instance
method of an arbitrary object
of a particular type
ContainingType::methodName Class::instanceMethodName
4. Reference to an instance
method of a particular object containingObject::instanceMethodName object::instanceMethodName
You can call the methods with their names
(references) with :: operator.
Arrays.sort(names, Test::matchStringLength);
3) Functional Interfaces
(new to jdk 1.8)
• Functional Interface have a single functionality
to exhibit.
• For example, a Comparable interface with a
single method ‘compareTo’ is used for
comparison purpose.
• Java 8 has defined a lot of functional
interfaces to be used extensively in lambda
expressions.
4) Default Methods (new to jdk 1.8)
• Java 8 introduces default method so that
List/Collection interface can have a default
implementation of forEach method,
• The class implementing these interfaces need
not implement the same.
Syntax
public interface vehicle {
default void print(){ System.out.println("I am a vehicle!");
} }
Multiple Defaults
• With default functions in interfaces, there is a
possibility that a class is implementing two
interfaces with same default methods.
public interface vehicle {
default void print(){
System.out.println("I am a vehicle!");
} }
public interface fourWheeler {
default void print(){
System.out.println("I am a four wheeler!");
} }
5) Optional (new to jdk 1.8)
• Optional is a container object which is used to
contain not-null objects.
• Optional object is used to represent null with
absent value.
• This class has various utility methods to
facilitate code to handle values as ‘available’
or ‘not available’ instead of checking null
values.
Class Declaration for java.util.Optional<T>
public final class Optional<T> extends Object
6) Parallel Sort (new to jdk 1.8)
Parallel sort
• Arrays#parallelSort uses Fork/Join framework
introduced in Java 7 to assign the sorting tasks
to multiple threads available in the thread
pool.
Difference between Arrays.sort &
Arrays.ParallelSort
1. Arrays.sort() : is a sequential sorting.
The API uses single thread for the operation.
The API takes bit longer time to perform the
operation.
2. Arrays.ParallelSort() : is a parallel sorting.
The API uses multiple threads.
The API takes lesser the time compared to Sort().
Why Parallel Sort?
• Both sort() and parallelSort() sorts the array.
• The performance with parallelSort() can be
seen when the number of arrays to sort are
very many.
7) Addition of Calender.Builder
(new to jdk 1.8)
• Before JDK 1.8, each date field is set
separately with individual methods.
• Each set method added as a separate
statement.
Calendar.Builder in jdk 1.8
Calendar calendar1 = new Calendar.Builder()
.set(Calendar.YEAR, 2013)
.set(Calendar.MONTH, 3)
.set(Calendar.DATE, 10)
.set(Calendar.HOUR, 8)
.set(Calendar.MINUTE, 56)
.set(Calendar.SECOND, 14)
.build();
• In JDK 1.8, Calendar.Builder is used to
instantiate calendar1 instance and all set
methods are used as a single statement.
• Semicolon is given only one
after build() method.
public static void DemoCalendarWithSingleSet() {
final Calendar calendar =
Calendar.getInstance(TimeZone.getTimeZone(timeZoneId), ENGLISH);
calendar.set(2013, APRIL, 6, 15, 45, 22);
out.println("Calendar via Constructor: " + stringifyCalendar(calendar));
}
Other new features
• Streams
• New Data/Time API
• Nashorn JavaScript
• Base 64
What’s gone ??
• Javax.swing.ImageIcon.Component
component
@Deprecated
• protected static final Component component
• Deprecated. since 1.8
• Do not use this shared component, which is used to track
image loading. It is left for backward compatibility only.
tracker
@Deprecated
• protected static final MediaTracker tracker
• Deprecated. since 1.8
• Do not use this shared media tracker, which is used to load
images. It is left for backward compatibility only.
Thank you!

Java- Updates in java8-Mazenet solution

  • 1.
    Updates in jdk1.8 By Sharmilee 9894303344 Java Trainer Mazenet Solution
  • 2.
    Objectives • What’s newwith JAVA -8 • What’s been modified with JAVA – 8 • What’s gone
  • 3.
  • 4.
    1. Lambda Expression 2.Method references 3. Functional Interface 4. Default method 5. Optional 6. Parallel sort 7. Calender.Builder
  • 5.
  • 6.
    private void sortUsingJava7(List<String>names) { Collections.sort(names, new Comparator<String>() { @Override public int compare(String s1, String s2) { return s1.compareTo(s2); } }); } private void sortUsingJava8(List<String> names) { Collections.sort(names, (s1, s2) -> s1.compareTo(s2)); }
  • 7.
    1) Lambda Introduction(new to jdk 1.8)
  • 8.
    What is lambda? •Lambda expression facilitates functional programming, and simplifies the development a lot
  • 9.
    Syntax of lambdaexpression • “ -> ” is known as lambda expression. parameter -> expression body
  • 10.
    Characteristics of Lambdaexpression • Optional type declaration • Optional parenthesis around parameter • Optional curly braces • Optional return keyword
  • 11.
    2) Method References(new to jdk 1.8)
  • 12.
    Method References • Ithelp to point to methods by their names. • A method reference is described using :: (double colon) symbol. • A method reference can be used to point the following types of methods − Static methods Instance methods Constructors using new operator (TreeSet::new)
  • 13.
    Types of methodreference Type Example Syntax 1. Reference to a static method ContainingClass::staticMethodName Class::staticMethodName 2. Reference to a constructor ClassName::new ClassName::new 3. Reference to an instance method of an arbitrary object of a particular type ContainingType::methodName Class::instanceMethodName 4. Reference to an instance method of a particular object containingObject::instanceMethodName object::instanceMethodName
  • 14.
    You can callthe methods with their names (references) with :: operator. Arrays.sort(names, Test::matchStringLength);
  • 15.
  • 16.
    • Functional Interfacehave a single functionality to exhibit. • For example, a Comparable interface with a single method ‘compareTo’ is used for comparison purpose. • Java 8 has defined a lot of functional interfaces to be used extensively in lambda expressions.
  • 17.
    4) Default Methods(new to jdk 1.8)
  • 18.
    • Java 8introduces default method so that List/Collection interface can have a default implementation of forEach method, • The class implementing these interfaces need not implement the same.
  • 19.
    Syntax public interface vehicle{ default void print(){ System.out.println("I am a vehicle!"); } }
  • 20.
    Multiple Defaults • Withdefault functions in interfaces, there is a possibility that a class is implementing two interfaces with same default methods.
  • 21.
    public interface vehicle{ default void print(){ System.out.println("I am a vehicle!"); } } public interface fourWheeler { default void print(){ System.out.println("I am a four wheeler!"); } }
  • 22.
    5) Optional (newto jdk 1.8)
  • 23.
    • Optional isa container object which is used to contain not-null objects. • Optional object is used to represent null with absent value. • This class has various utility methods to facilitate code to handle values as ‘available’ or ‘not available’ instead of checking null values.
  • 24.
    Class Declaration forjava.util.Optional<T> public final class Optional<T> extends Object
  • 25.
    6) Parallel Sort(new to jdk 1.8)
  • 26.
    Parallel sort • Arrays#parallelSortuses Fork/Join framework introduced in Java 7 to assign the sorting tasks to multiple threads available in the thread pool.
  • 27.
    Difference between Arrays.sort& Arrays.ParallelSort 1. Arrays.sort() : is a sequential sorting. The API uses single thread for the operation. The API takes bit longer time to perform the operation. 2. Arrays.ParallelSort() : is a parallel sorting. The API uses multiple threads. The API takes lesser the time compared to Sort().
  • 28.
    Why Parallel Sort? •Both sort() and parallelSort() sorts the array. • The performance with parallelSort() can be seen when the number of arrays to sort are very many.
  • 29.
    7) Addition ofCalender.Builder (new to jdk 1.8)
  • 30.
    • Before JDK1.8, each date field is set separately with individual methods. • Each set method added as a separate statement.
  • 31.
    Calendar.Builder in jdk1.8 Calendar calendar1 = new Calendar.Builder() .set(Calendar.YEAR, 2013) .set(Calendar.MONTH, 3) .set(Calendar.DATE, 10) .set(Calendar.HOUR, 8) .set(Calendar.MINUTE, 56) .set(Calendar.SECOND, 14) .build();
  • 32.
    • In JDK1.8, Calendar.Builder is used to instantiate calendar1 instance and all set methods are used as a single statement. • Semicolon is given only one after build() method.
  • 33.
    public static voidDemoCalendarWithSingleSet() { final Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone(timeZoneId), ENGLISH); calendar.set(2013, APRIL, 6, 15, 45, 22); out.println("Calendar via Constructor: " + stringifyCalendar(calendar)); }
  • 34.
    Other new features •Streams • New Data/Time API • Nashorn JavaScript • Base 64
  • 35.
    What’s gone ?? •Javax.swing.ImageIcon.Component component @Deprecated • protected static final Component component • Deprecated. since 1.8 • Do not use this shared component, which is used to track image loading. It is left for backward compatibility only. tracker @Deprecated • protected static final MediaTracker tracker • Deprecated. since 1.8 • Do not use this shared media tracker, which is used to load images. It is left for backward compatibility only.
  • 36.