Features of Java8 explained

Hi all time is passing so fast that Oracle has published its road map for “Java 9”, now in this post let me explain the best features that are available in Java 8.
Java 8 allow us to write a non abstract methods inside the interface by utilizing the keyword “default” below is the code snippet.

/**
 * 
 */
package com.spark.java8.tutorial.interfaces;

/**
 * @author Sony
 *
 */
public interface ICalculator {

	double getSquareOf(double a);
	
	default double getSquareRoot(double a){
		return Math.sqrt(a);
	}
}

The implementation of the above said interface can be done as shown below.

/**
 * 
 */
package com.spark.java8.tutorial.interfaces.impl;

import com.spark.java8.tutorial.interfaces.ICalculator;

/**
 * @author Sony
 *
 */
public class MyCalculator implements ICalculator {

	/* (non-Javadoc)
	 * @see com.spark.java8.tutorial.interfaces.ICalculator#getSquareOf(double)
	 */
	@Override
	public double getSquareOf(double a) {
		return a*a;
	}

}

To test the above implementation and default implementation method of interface, write the below code in main class and run.

                MyCalculator myCalculator = new MyCalculator();
		double result1 = myCalculator.getSquareOf(10);
		System.out.println(result1);
		double result2 = myCalculator.getSquareRoot(4);
		System.out.println(result2);

Let us start with Lambda Expression: let us have a look at how to sort a list of strings in prior versions of Java

List<String> names = Arrays.asList("Vishal","Keshav","Pavan Kumar","Sudheer","Achary","Kranthi","Deepak");
		
		Collections.sort(names,new Comparator<String>() {
			@Override
			public int compare(String o1, String o2) {
				return o2.compareTo(o1);
			}
		});
		System.out.println(names);

Instead of creating anonymous objects all day long, Java 8 comes with a much shorter syntax, lambda expressions:

Collections.sort(names, (String a, String b) -> {
    return b.compareTo(a);
});

the above code can be more simplified as follows.

Collections.sort(names, (String a, String b) -> b.compareTo(a));

For single line method bodies you can skip braces {} and the return keyword. the above can be made still more shoter as below.

Collections.sort(names, (a,b) -> a.compareTo(b));
		System.out.println(names);

Functional Interfaces:
An interface with only one abstract method is called functional interface. Java 8 provides us with an annotation called @FunctionalInterface to make any interface as functional interface, also in java 8 we can pass Method and Constructor references to functional interfaces below are the code snippets which demonstrate this concept.

/**
 * 
 */
package com.spark.java8.tutorial.interfaces;
/**
 * @author Sony
 *
 */
@FunctionalInterface
public interface Converter<F,T> {
	T convert(F from);
}

CharecterOperations.java

/**
 * 
 */
package com.spark.java8.test;

/**
 * @author Sony
 *
 */
public class CharecterOperations {

	public String getFirstChar(String str){
		return String.valueOf(str.charAt(0));
	}
}

using the above interface and class definition we can do something as below.

Converter<String, Integer> converter = Integer::valueOf;
		Integer value = converter.convert("123");
		System.out.println(value+10);

CharecterOperations charecterOperations = new CharecterOperations();
		Converter<String, String> converter2 = charecterOperations::getFirstChar;
		String val = converter2.convert("Java8");
		System.out.println(val);

There are lot many features available in Java 8 like “Scopes in lambda Expressions”, “Built in Functional Interfaces”, “Streams in Collections” and many more let us look them in my next post.

Happy Coding 🙂
Happy Java 8.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s