Learn Java 8

Here we would understand Java 8 from the basics of course we are aware of the syntax of old versions of java

A simple java program..!!

public class HelloWorld  
{ 
 public static void main(String[] args)
{ 
 System.out.println("Hello World"); 
 } 
 }

Hope everybody are aware of this Syntax.

Many of the features are included in java 8 but here we will study some of them.
The very basic and first and foremost is lambda expression. 

lambda expression Adds functional processing capability to Java.They are considered
to be biggest feature of java 8.

Its basic syntax is : parameter -> expression body 
Some of the advantages of JAVA 8 are as follows 

1.Type parameter are not required ,predicts from the type of value provided.
2.parenthesis are optional for parameter if it is single for multiple it is required.!
3.optional curly braces same as parameter here if only one statement exist no
need of curly braces.
4.no need to mention the return type it automatically returns value containing an
  expression returning value .

//java8 example
public class Java8Tester 
 {   
final static String salutation = "Hello! ";
  public static void main(String args[])
{
  GreetingService gs = message -> System.out.println(salutation + message); gs.sayMessage("Mahesh");  
}
  interface GreetingService {  
void sayMessage(String message);
  }
  }

 explanation of above program: 

Here we have a class named Java8 Tester in it we have taken a final String named hello that is it cannot be changed. Here we have our main method where the execution of JAVA program starts.
Here just keep the Scope as it is outside the Scope of main define one Interface named 
GreetingService we have one method in it void sayMessage(String message); 
we cannot define that method in interface but we can declare
Now come back to main method here we will declare one refrence say gs 
now gs= on the right hand side of equal to we have parameter as said eariler,
we dont have to do parenthesis if we have one parameter neither we have written
String message it calculates automatically on the value passed to it.
--> next to arrow our body of function or method starts.In that we have printed 
one message.Now messaage we have to print comes by calling that method.
we can call sayMessage by that refrence, we created that is 
gs.sayMessage("anymessage");
 
OR WE CAN WRITE LIKE THIS

public class LambdaHelloWorld  
{
  interface HelloWorld { 
 String hello(String name);  
} 
 public static void main(String[] args) {
 HelloWorld  haha = (String name) -> { return "Hello " + name; }; System.out.println(haha.hello("kushal"));
  }
  }
 
Method reference in Java 8 is the ability to use a method as an argument for a matching functional interface. :: (double colon) is the operator used for method reference in Java. An interface with only one method is called a functional interface. For example, Comparable, Runnable, AutoCloseable are some functional interfaces in Java. 
Here are some other examples
1.() -> { System.out.println("Hello World!");}  
2.(int a, int b) -> a + b  
3.() -> { return 1; } 
4.(String name) -> { System.out.println("Hello "+name); }
5. n -> n % 2 != 0


Now we will try this example


package com.techopedia.java8; 
public class MethodReferenceExample
{
 void close() { 
System.out.println("Close."); 

public static void main(String[] args) throws Exception { 
MethodReferenceExample myobj = new MethodReferenceExample(); 
try (AutoCloseable ac = myobj::close) { 
}
 }
 }  

AutoCloseable interface we have the method of signature as,
void close throws Exception() so we have this example here before we have made interface by our own this interface here we have void close throws Exception()

Here explicitly we have defined method void close(); which has same name and same return Type..!

Therefore we can use the method we have written for the functional interface 
AutoCloseable as a method reference.
BY AutoCloseable ac = myobj::close--> the thing in bold is passing method as 
arguement :: is convience operator which we called Scope resolution in C CPP.
This means that we have assigned a method reference in an interface refrence.
obviously we can call it by ac.close().
In the next post we will see the looping examples and other concepts in java 8



Comments

Popular posts from this blog

State space search / blocks world problem by heuristic approach /TIC TAC TOE

Navigation in Vaadin.

Drag and drop items from one Grid to another