Java New Features - Complete Exam-Friendly Notes with Syntax, Examples & Programs
1. Functional Interfaces
• Definition: An interface with only one abstract method.
• Syntax:
@FunctionalInterface
interface MyInterface {
void show();
}
• Use: Supports Lambda Expressions.
• Example Program:
@FunctionalInterface
interface Greeting {
void sayHello();
}
public class Main {
public static void main(String[] args) {
Greeting g = () -> [Link]("Hello!");
[Link]();
}
}
2. Lambda Expression
• Definition: Anonymous function that implements a functional interface.
• Syntax: (parameter) -> { body }
• Example:
Runnable r = () -> [Link]("Lambda Running");
[Link]();
3. Method Reference
• Definition: Reference to method using :: operator.
• Syntax: ClassName::methodName
1
• Example:
public class Test {
public static void sayHi() {
[Link]("Hi!");
}
public static void main(String[] args) {
Runnable r = Test::sayHi;
[Link]();
}
}
4. Stream API
• Definition: Functional-style operations on collections.
• Example:
List<String> list = [Link]("A", "B", "C");
[Link]().forEach([Link]::println);
• Operations: map() , filter() , collect() , forEach()
5. Default Method (in Interface)
• Definition: Allows method body in interface.
• Syntax:
default void show() {
[Link]("Default Method");
}
6. Static Method (in Interface)
• Definition: Static method inside interface.
• Example:
interface MyInterface {
static void display() {
[Link]("Static in Interface");
2
}
}
7. Base64 Encoding/Decoding
• Definition: Encode and decode data using Base64.
• Program:
import [Link].Base64;
public class EncodeDemo {
public static void main(String[] args) {
String original = "Hello";
String encoded =
[Link]().encodeToString([Link]());
[Link](encoded);
byte[] decodedBytes = [Link]().decode(encoded);
[Link](new String(decodedBytes));
}
}
8. forEach() Method
• Definition: Looping function in Stream/Iterable.
• Example:
List<String> list = [Link]("One", "Two");
[Link](item -> [Link](item));
9. Try-with-resources
• Definition: Auto close resources (e.g., File).
• Example:
try (BufferedReader br = new BufferedReader(new FileReader("[Link]"))) {
[Link]([Link]());
}
3
10. Type Annotations
• Definition: Annotations applied to types.
• Example:
@NonNull String name;
11. Repeating Annotations
• Definition: Use same annotation multiple times.
• Example:
@Hint("hint1")
@Hint("hint2")
class Demo {}
12. Java Module System (Java 9)
• Definition: Organize code into modules.
• Syntax:
module [Link] {
requires [Link];
exports [Link];
}
13. Diamond Operator in Anonymous Class
• Definition: Use <> in anonymous class.
• Example:
List<String> list = new ArrayList<>() {
// anonymous class
};
14. Local Variable Type Inference (Java 10)
• Definition: Use var for local variables.
4
• Example:
var name = "Java";
[Link](name);
15. Switch Expressions (Java 14)
• Definition: Switch as an expression.
• Syntax:
String dayType = switch(day) {
case MONDAY, FRIDAY -> "Weekday";
default -> "Weekend";
};
16. Yield Keyword
• Definition: Return a value from switch block.
• Example:
int num = 2;
String result = switch(num) {
case 1 -> {
yield "One";
}
default -> {
yield "Other";
}
};
17. Text Blocks (Java 15)
• Definition: Multiline string using """ .
• Example:
String json = """
{
"name": "Afjal",
"age": 21
5
}
""";
18. Records (Java 14)
• Definition: Concise syntax for data classes.
• Syntax:
record Student(String name, int age) {}
• Usage: Auto-generates constructor, getters, toString() .
19. Sealed Classes (Java 17)
• Definition: Restricts which classes can extend.
• Syntax:
sealed class Shape permits Circle, Square {}
final class Circle extends Shape {}
final class Square extends Shape {}
Quick Revision Table
Feature Introduced In Key Use
Functional Interface Java 8 Supports Lambda
Lambda Expression Java 8 Short functions
Stream API Java 8 Process collections
Method Reference Java 8 Call methods using ::
Default/Static in Interface Java 8 Method body in interface
Base64 Java 8 Encode/Decode strings
forEach() Java 8 Iteration shortcut
Try-with-resources Java 7 Auto-close resource
Type Annotations Java 8 Meta-info for types
Repeating Annotations Java 8 Use multiple times
6
Feature Introduced In Key Use
Java Module System Java 9 Modular programming
Diamond in Anonymous Java 9 Short syntax
var keyword Java 10 Type inference
Switch Expressions Java 14 Returns value
yield Java 14 Used inside switch
Text Blocks Java 15 Multi-line strings
Records Java 14 Data-only classes
Sealed Classes Java 17 Restricted inheritance
Let me know if you want a PDF version, mock tests, or flashcards for these!