What are the new features of java-8?

On March 18, 2014, Oracle released a new version of Java as Java 8. It was a revolutionary release of Java for the software development platform. Following are the new features of java-8.

  1. Static and default methods in the interface
  2. Functional programming.
  3. Lambda Expressions
  4. Stream API
  5. For Each loop
  6. New Date Time API
  7. Method Reference: (passing a function to a function)
  1. Define methods In interface
  • default Methods in the interface.
    • We can define the default method inside the interface like
  • That method should not be the same as the Object class method we can not define the equals method, It will give an error. 
  • If the class is implementing both the interfaces having the same default method then that class must override the method inside it to avoid ambiguity problem
  • Static methods in the interface
    • We can also write static methods inside the interface
Java Code
class Test {
public static void main(String[] args) {
C c = new C();
c.greet();
c.showData(“CBXS”);
A.message(“This is ugtworld.com”);
}
}

interface A {
public void greet();

default void showData(String username) {
System.out.println(username);
}

static void message(String message) {
System.out.println(message);
}

}

class C implements A {

public void greet() {
System.out.print(“Hello “);
}
}

Output:
Hello CBXS
This is ugtworld.com

  1. Functional programming
  • Until java 8, java was supporting an object-oriented style of programming. Java started supporting the functional style of programming from java-8.
  • Now we can pass, create or return any object or function. Lambda expressions are used for the functional interface.
  1. Lambda Expressions
  • Only applicable for functional interfaces (Interface which has only one abstract method)
  • If Interface has only one abstract method then there is no need to create the implementation class we can directly use it in our code using lambda expressions.
  • In the example below As we created implementation class C in the previous example, no implementation class is created or no anonymous class is created.
Java Code
class Test {
public static void main(String[] args) {
A obj = () -> System.out.print(“Hello “);
obj.greet();
obj.showData(“CBXS”);
A.message(“This is ugtworld.com”);
}
}

interface A {
public void greet();

default void showData(String username) {
System.out.println(username);
}

static void message(String message) {
System.out.println(message);
}

}

Output:
Hello CBXS
This is ugtworld.com

  1. Stream API
  • Stream
    • Helps to achieve concurrency and to achieve functional programming. (Declarative Programming)
    • We can not reuse Stream once it is used.
  • Parallel Stream
    •  It helps in processing the collected data in parallel. This used multiple threads,  so no guaranteed sequence of output.
  1. For Each loop
    1. In java, the loops we are using to iterate over any collection object are external loops.
    2. Java-8 introduced a new foreach loop which is an internal loop to iterate over any collection object as shown in the example below.
Java Code
import java.util.Arrays;
import java.util.List;

class Test {
public static void main(String[] args) {
List values = Arrays.asList(1,2,3,4,5,6);
values.forEach(i -> System.out.println(i));
}
}

  • i -> sysout(i) used as an argument is actually a lambda expression and it is a Consumer interface.
  1. New Date Time API
  • The old date and time are not thread-safe. The old date is available in two packages, java.util and java.sql
  • The new java-8 date time API is immutable and present in java.time package
Java Code
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;

class Test {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
System.out.println(date);
LocalTime time = LocalTime.now();
System.out.println(time);

//set of zones
ZoneId.getAvailableZoneIds().forEach(System.out::println);

//zone is got from the above set
LocalTime zoneTime1 = LocalTime.now(ZoneId.of(“Asia/Kolkata”));
System.out.println(“India: ” + zoneTime1);

//zone is got from the above set
LocalTime zoneTime2 = LocalTime.now(ZoneId.of(“Asia/Seoul”));
System.out.println(“South Korea: “+ zoneTime2);

//Human variable time
LocalDateTime dateTime = LocalDateTime.now();
System.out.println(dateTime);

//GMT Time Z is attached at the end of the time machine variable time
Instant i = Instant.now();
System.out.println(i);
}
}

  1. Method Reference: 
  • Java-8 also introduced method reference which means passing a function to a function, as shown below
    • ZoneId.getAvailableZoneIds().forEach(System.out::println);
  • forEach is a method that takes method which is println(i) so instead of doing like the below code, we can use the method reference to print the value as above.
    • ZoneId.getAvailableZoneIds().forEach(i -> System.out.println(i));
  • Method reference is used by :: 
  • In the above example, we are calling the method of System.out Where System is class and out is an object of PrintStream created in the System class.

-A blog by Shwetali Khambe

Related Posts