Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Java8 Lambda Expressions example in Eclipse

Related Tutorials


 
 

Share on Google+Share on Google+

Lambda Expressions example in Eclipse

Advertisement
In this section you will learn how to use the Eclipse IDE for running the Lambda Expression example. In this first Lambda Expression example we will show you how to write simple hello world example using new Lambda expressions.

Lambda Expression example in Java 8 using Eclipse IDE

Learn how to download the JDK 8 RC build and configure it with Eclipse IDE. You will also learn how to write your first Lambda Expression example in Java 8.

First of all we will show you how you can set-up the development environment using Java 8 RC release software and then configure the Eclipse IDE. We will show you how to download the Eclipse for using the JDK 8 for testing the application.

You will be needing the Eclipse IDE which supports the JDK 8.

First of all download the JDK 8 RC build from https://jdk8.java.net/. Following screen shot shows the link to download the JDK 8 Release Candidate:

Download JDK 8 release candidate

You should download the file by clicking on the above link.

Double click the downloaded file and then install and configure it on your computer. You should configure the JAVA_HOME and PATH variables on your computer.

Now you how to download the JDK 8 supporting eclipse IDE. Following is the URL to download the JDK 8 supporting Eclipse IDE:

Here is the screen shot of the website:

JDK 8 Supporting Eclipse

Download and extract the content of the file. Now click on the "eclipse.exe" to run the eclipse IDE.

Then go to Windows -> Preferences -> Compiler as shown below:

JDK 8 configuration in Eclipse

Here you have to select the Compiler compliance level: 1.8(BETA) for using the JDK 8.

Now we will write simple Hello world Lambda Expression in JDK 8.

Create a project in Eclipse IDE and then create a new class "Test.java" as shown below:

Lambda Expression Hello world example in Java

Now add the following code to create an interface:

interface HelloService {
      String hello(String firstname, String lastname);
}

We will use the Lambda Expression. Here is the complete code:

package net.roseindia;

public class Hello {
	interface HelloService {
	      String hello(String firstname, String lastname);
	   }
	
	public static void main(String[] args) {
		HelloService helloService = (String firstname, String lastname) -> {
			String hello = "Hello " + firstname + " " + lastname;
			return hello;
		};
		System.out.println(helloService.hello("Deepak", "Kumar"));	
	}
}

If you run the above code it will print "Hello Deepak Kumar" message on the console.

Here is example code of the Lambda Expression:

HelloService helloService = (String firstname, String lastname) -> {
			String hello = "Hello " + firstname + " " + lastname;
			return hello;
		};

Syntax of Lambda Expression:

Following is the syntax of the Lambda Expression:

(parameters) ->{ expression/statements }

In the above code (String firstname, String lastname) is the parameter list followed by the "->" sign which is used for Lambda Expression. And the statements are enclosed in the {} brackets.

Check more tutorials of Lambda Expression in Java.

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 20, 2014

Related Tutorials

Discuss: Lambda Expressions example in Eclipse  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com