Java8 Features
Java8 Features
JAVA 8 Features
Java 8 Features
1. Lambda Expressions
2. Functional Interfaces
3. Streams API
4. Default and Static Methods in Interfaces
5. Optional Class
6. Date and Time API (java.time package)
7. Nashorn JavaScript Engine
8. Method References
9. Collectors Class
10.Concurrency Enhancements
11.Comparable and Comparator
12.Annotations on Java Types
13.Repeating Annotations
14.New JavaFX Features
15.Enhanced Security Features
16.Parallel Array Sorting
17.Base64 Encoding and Decoding
18.PermGen Space Removal
19.Java Mission Control (JMC)
20.Compact Profiles
21.Unsigned Arithmetic Operations
1. Lambda Expressions
Example:
java
// Before Java 8
Runnable r1 = new Runnable() {
@Override
public void run() {
System.out.println("Hello from a runnable!");
}
};
r1.run();
r2.run();
2. Functional Interfaces
java
@FunctionalInterface
interface MyFunctionalInterface {
void doSomething();
}
3. Streams API
Introduced in Java 8, streams encourage concise and readable code for data
manipulation and transformation tasks.
Example:
java
import java.util.Arrays;
import java.util.List;
names.stream()
.filter(name -> name.startsWith("J"))
.map(String::toUpperCase)
.forEach(System.out::println);
Default and static methods enable the development of more robust and
reusable code in Java APIs.
Example:
java
interface MyInterface {
default void defaultMethod() {
System.out.println("This is a default
method");
}
5. Optional Class
The Optional class in Java is used to represent a value that may or may not
be present.
Example:
java
import java.util.Optional;
optional.ifPresent(System.out::println); // Prints
"Hello"
System.out.println(optional.orElse("Default")); //
Prints "Hello"
Example:
java
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
Example:
java
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
try {
engine.eval("print('Hello from JavaScript')");
} catch (ScriptException e) {
e.printStackTrace();
}
8. Method References
Example:
java
import java.util.Arrays;
9. Collectors Class
Example:
java
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
Example:
java
import java.util.concurrent.CompletableFuture;
Comparable
The Comparable interface is used to define the natural ordering of objects. It has
a single method, compareTo, which compares the current object with another
object of the same type.
Example:
java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@Override
public int compareTo(Student other) {
return Integer.compare(this.age, other.age); //
Compare based on age
}
@Override
public String toString() {
return name + " (" + age + ")";
}
Collections.sort(students);
System.out.println(students); // Output: [Doe
(20), Jane (22), John (25), Jack (28)]
}
}
Comparator
Example:
java
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
Key Points
Annotations provide metadata about Java code, which can be used by the
compiler or at runtime.
Example:
java
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
@Target(ElementType.TYPE_USE)
@interface MyAnnotation {}
They are useful when multiple instances of the same annotation need to be
associated with a program element, such as specifying multiple roles or
permissions for a method or class.
Example:
java
import java.lang.annotation.*;
@Repeatable(MyAnnotations.class)
@interface MyAnnotation {
String value();
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface MyAnnotations {
MyAnnotation[] value();
}
@MyAnnotation("First")
@MyAnnotation("Second")
public class Example {}
JavaFX is a platform for creating rich internet applications (RIAs) and GUI
applications in Java.
JavaFX provides a rich set of APIs for multimedia, animation, and user
interface development, making it suitable for building modern desktop and
mobile applications.
Java 8 introduced parallel sorting for arrays, utilizing multiple threads for
faster sorting of large arrays.
The Arrays.parallelSort() method divides the array into smaller parts and
sorts them concurrently using the Fork/Join framework.
It is suitable for sorting operations where the data set is large and sorting can
be done independently on different segments of the array.
Example:
java
import java.util.Arrays;
Java provides built-in support for Base64 encoding and decoding through
classes in the java.util package.
Base64 encoding is used for encoding binary data into text format, which is
useful for transmitting data over text-based protocols like HTTP.
Example:
java
import java.util.Base64;
System.out.println(encoded); // Output:
SGVsbG8sIFdvcmxkIQ==
System.out.println(decoded); // Output: Hello, World!
Starting from Java 8, PermGen space was removed and replaced with the
Metaspace to improve memory management and reduce issues related to
PermGen space exhaustion.
Java Mission Control is a tool for monitoring, managing, and profiling Java
applications.
Compact Profiles in Java provide subsets of the Java SE platform tailored for
different deployment scenarios.
They include selected APIs necessary for specific application types or
devices, reducing the footprint and improving startup time of Java applications.
Java traditionally does not support unsigned data types (like unsigned int)
found in some other programming languages.
Example:
java
int a = -1;
System.out.println(Integer.toUnsignedString(a)); //
Output: 4294967295