JAVA 8 (aka jdk 1.8) is a major release of JAVA programming language development. Its initial version was released on 18 March 2014. With the Java 8 release, Java provided support for functional programming, new JavaScript engine, new APIs for date time manipulation, new streaming API, etc.
পৃষ্ঠাসমূহ
Labels
Search Your Article
CS
Pageviews
Showing posts with label Java 8 Tutorial. Show all posts
Showing posts with label Java 8 Tutorial. Show all posts
Friday, February 3, 2017
Java 8 - Environment Setup
Try it Option Online
We have set up the Java Programming environment online, so that you can compile and execute all the available examples online. It gives you confidence in what you are reading and enables you to verify the programs with different options.
Java 8 - Lambda Expressions
Lambda expressions are introduced in Java 8 and are touted to be the
biggest feature of Java 8. Lambda expression facilitates functional
programming, and simplifies the development a lot.
Java 8 - Method References
Method references help to point to methods by their names. A method reference is described using
:: (double colon) symbol. A method reference can be used to point the following types of methods −Java 8 - Functional Interfaces
Functional interfaces have a single functionality to exhibit. For
example, a Comparable interface with a single method ‘compareTo’ is used
for comparison purpose. Java 8 has defined a lot of functional
interfaces to be used extensively in lambda expressions. Following is
the list of functional interfaces defined in java.util.Function package.
Java 8 - Default Methods
Java 8 introduces a new concept of default method implementation in
interfaces. This capability is added for backward compatibility so that
old interfaces can be used to leverage the lambda expression capability
of Java 8.
Java 8 - Streams
Stream is a new abstract layer introduced in Java 8. Using stream,
you can process data in a declarative way similar to SQL statements. For
example, consider the following SQL statement −
SELECT max(salary), employee_id, employee_name FROM Employee
Java 8 - Optional Class
Optional is a container object which is used to contain not-null
objects. Optional object is used to represent null with absent value.
This class has various utility methods to facilitate code to handle
values as ‘available’ or ‘not available’ instead of checking null
values. It is introduced in Java 8 and is similar to what Optional is in Guava.
Note − This class inherits methods from the java.lang.Object class.
Class Declaration
Following is the declaration for java.util.Optional<T> class −public final class Optional<T> extends Object
Class Method
| S. No. | Method & Description |
|---|---|
| 1 | static <T> Optional<T> empty() Returns an empty Optional instance. |
| 2 | boolean equals(Object obj) Indicates whether some other object is "equal to" this Optional. |
| 3 | Optional<T> filter(Predicate<? super <T> predicate) If a value is present and the value matches a given predicate, it returns an Optional describing the value, otherwise returns an empty Optional. |
| 4 | <U> Optional<U> flatMap(Function<? super T,Optional<U>> mapper) If a value is present, it applies the provided Optional-bearing mapping function to it, returns that result, otherwise returns an empty Optional. |
| 5 | T get() If a value is present in this Optional, returns the value, otherwise throws NoSuchElementException. |
| 6 | int hashCode() Returns the hash code value of the present value, if any, or 0 (zero) if no value is present. |
| 7 | void ifPresent(Consumer<? super T> consumer) If a value is present, it invokes the specified consumer with the value, otherwise does nothing. |
| 8 | boolean isPresent() Returns true if there is a value present, otherwise false. |
| 9 | <U>Optional<U> map(Function<? super T,? extends U> mapper) If a value is present, applies the provided mapping function to it, and if the result is non-null, returns an Optional describing the result. |
| 10 | static <T> Optional<T> of(T value) Returns an Optional with the specified present non-null value. |
| 11 | static <T> Optional<T> ofNullable(T value) Returns an Optional describing the specified value, if non-null, otherwise returns an empty Optional. |
| 12 | T orElse(T other) Returns the value if present, otherwise returns other. |
| 13 | T orElseGet(Supplier<? extends T> other) Returns the value if present, otherwise invokes other and returns the result of that invocation. |
| 14 | <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) Returns the contained value, if present, otherwise throws an exception to be created by the provided supplier. |
| 15 | String toString() Returns a non-empty string representation of this Optional suitable for debugging. |
Optional Example
To understand how Optional is used in practice, let us see the following example. Write the following program, execute and verify result to get more insight of it.Java8Tester.java
import java.util.Optional; public class Java8Tester { public static void main(String args[]){ Java8Tester java8Tester = new Java8Tester(); Integer value1 = null; Integer value2 = new Integer(10); //Optional.ofNullable - allows passed parameter to be null. Optional<Integer> a = Optional.ofNullable(value1); //Optional.of - throws NullPointerException if passed parameter is null Optional<Integer> b = Optional.of(value2); System.out.println(java8Tester.sum(a,b)); } public Integer sum(Optional<Integer> a, Optional<Integer> b){ //Optional.isPresent - checks the value is present or not System.out.println("First parameter is present: " + a.isPresent()); System.out.println("Second parameter is present: " + b.isPresent()); //Optional.orElse - returns the value if present otherwise returns //the default value passed. Integer value1 = a.orElse(new Integer(0)); //Optional.get - gets the value, value should be present Integer value2 = b.get(); return value1 + value2; } }
Verify the Result
Compile the class using javac compiler as follows −$javac Java8Tester.javaNow run the Java8Tester as follows −
$java Java8TesterIt should produce the following output −
First parameter is present: false Second parameter is present: true 10
Java 8 - Nashorn JavaScript
With Java 8, Nashorn, a much improved javascript engine is
introduced, to replace the existing Rhino. Nashorn provides 2 to 10
times better performance, as it directly compiles the code in memory and
passes the bytecode to JVM. Nashorn uses invokedynamics feature, introduced in Java 7 to improve performance.
Java 8 - New Date/Time API
With Java 8, a new Date-Time API is introduced to cover the following drawbacks of old date-time API −
- Not thread safe − java.util.Date is not thread safe, thus developers have to deal with concurrency issue while using date. The new date-time API is immutable and does not have setter methods.
Java 8 - Base64
With Java 8, Base64 has finally got its due. Java 8 now has inbuilt encoder and decoder for Base64 encoding. In Java 8, we can use three types of Base64 encoding −
Java 8 Questions and Answers
Java 8 Questions and Answers has been designed with a special intention of helping students and professionals preparing for various Certification Exams and Job Interviews. This section provides a useful collection of sample Interview Questions and Multiple Choice Questions (MCQs) and their answers with appropriate explanations.
Java 8 - Quick Guide
Java 8 - Overview
JAVA 8 is a major feature release of JAVA programming language development. Its initial version was released on 18 March 2014. With the Java 8 release, Java provided supports for functional programming, new JavaScript engine, new APIs for date time manipulation, new streaming API, etc.Java 8 - Useful Resources
The following resources contain additional information on Java8. Please use them to get more in-depth knowledge on this tpoic.
Discuss Java 8
Java 8 is the most awaited and is a major feature release of Java
programming language. This is an introductory tutorial that explains the
basic-to-advanced features of Java 8 and their usage in a simple and
intuitive way.
Thursday, January 19, 2017
Java 8 - Overview
JAVA 8 (aka jdk 1.8) is a major release of JAVA programming language
development. Its initial version was released on 18 March 2014. With the
Java 8 release, Java provided support for functional programming, new
JavaScript engine, new APIs for date time manipulation, new streaming
API, etc.
Java 8 - Environment Setup
Try it Option Online
We have set up the Java Programming environment online, so that you can compile and execute all the available examples online. It gives you confidence in what you are reading and enables you to verify the programs with different options. Feel free to modify any example and execute it online.
Java 8 - Lambda Expressions
Lambda expressions are introduced in Java 8 and are touted to be the
biggest feature of Java 8. Lambda expression facilitates functional
programming, and simplifies the development a lot.
Java 8 - Method References
Method references help to point to methods by their names. A method reference is described using
:: (double colon) symbol. A method reference can be used to point the following types of methods −Java 8 - Functional Interfaces
Functional interfaces have a single functionality to exhibit. For
example, a Comparable interface with a single method ‘compareTo’ is used
for comparison purpose. Java 8 has defined a lot of functional
interfaces to be used extensively in lambda expressions. Following is
the list of functional interfaces defined in java.util.Function package.
Subscribe to:
Posts (Atom)