Java Features JDK 8





Features added in Java8.0 :

This version was release in 2014 includes the following updates in the language:

  • default and static methods in Interfaces :
    From Java 8, interfaces are enhanced to have method with implementation. We can use default and static keyword to create interfaces with method implementation.

  • Example:
    interface Demo
    {
     default void show(){
      System.out.println("DEF-SHOW");
     }
     static void put(){
      System.out.println("STAT-PUT");
     }
     void get();
    }
    class MyDemo implements Demo
    {
     @Override
     public void get()
     {
      System.out.println("OVR-GET"); 
     }
    }
    class Test 
    {
     public static void main(String[] ar)
     {
      Demo d = new MyDemo();
      d.get();
      d.show();
      Demo.put();
     }
    }

  • forEach() method in Iterable interface :
    Whenever we need to traverse through a Collection, we need to create an Iterator whose whole purpose is to iterate over and then we have business logic in a loop for each of the elements in the Collection.
    Java 8 has introduced forEach method in java.lang.Iterable interface so that while writing code we focus on business logic only. forEach method takes java.util.function.Consumer object as argument, so it helps in having our business logic at a separate location that we can reuse.

  • Example:
    import java.util.function.Consumer;
    import java.util.ArrayList;
    class Test 
    {
     public static void main(String[] ar)
     {
      ArrayList< String> al;
      al = new ArrayList< String>();
      al.add("Ajay");
      al.add("Alok");
      al.add("Karan");
      al.add("Ayaan");
      al.forEach(new Consumer< String>()
      {
       public void accept(String s){
        s = s.toUpperCase();
        System.out.println(s);
      }
      });
     }
    }

  • Lambda Expression : If an interface contains only one abstract method then such interface are called functional interface. Since functional interfaces have only one method, lambda expressions can easily provide the method implementation. We just need to provide method arguments and business logic.
    So lambda expressions are means to create anonymous classes of functional interfaces easily. There are no runtime benefits of using lambda expressions.
    A new package java.util.function has been added with bunch of functional interfaces to provide target types for lambda expressions and method references.

  • Example:
    @FunctionalInterface
    interface Demo
    {
     public void prowess(int x);
    }
    class Test 
    {
     public static void main(String[] ar)
     {
      Demo d1,d2,d3;
      d1 = (int x)->System.out.println("C");
      d2 = (x)->System.out.println("C++");
      d3 =  x->System.out.println("Java");
      
      d1.prowess(11);
      d2.prowess(12);
      d3.prowess(13);
     }
    }

  • Method references : Method references helps to point to method by their name. A method reference is described using "::" symbol. A method referrence can be used to point following types of methods.

  • Example
    import java.util.*;
    class Test 
    {
     public static void main(String args[])
     {
      List names = new ArrayList();
      names.add("Mahesh");
      names.add("Suresh");
      names.add("Ramesh");
      names.add("Naresh");
      names.add("Kalpesh");
    
      names.forEach(System.out::println);
     }   
    }
    

  • Improvement over DATE-TIME API :
    JAVA 8 introduces a new date time API under package java.time. Following are some of the important classes introduced in java.time package.
    java.util.Date is not thread safe, thus developer have to deal with concurrency issue while using date. New date time API are immutable and do not have setter methods.

  • Example
    import java.time.*;
    class Test 
    {
     public static void main(String[] ar)
     {
     LocalDate date = LocalDate.now();
     LocalTime time = LocalTime.now();
     LocalDateTime dt = LocalDateTime.now();
     
     //2017-04-16
     System.out.println(date);
    
     //10:50:51.685
     System.out.println(time);
    
     //2017-04-16T10:50:51.685
     System.out.println(dt);
     }
    }


 






Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in



CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-8527238801 , +91-9451396824

© 2017, prowessapps.in, All rights reserved