java unit 3
java unit 3
Functional Interface
Lambda Expression
For-Each Method
Switch Expression
Sealed Classes
Autoboxing
Generics
Varargs
Java Annotations
Method References
Stream API
Try-with-Resources
Diamond Syntax
Yield Keyword
Text Blocks
Java Records
Java Module System
Base64 Encoding/Decoding
The main focus of this unit is on the newer additions, predominantly from
Java 8, 9, 10, 13, 14, and 15.
Key Concepts:
@FunctionalInterface
interface MyInterface {
System.out.println("Doing something");
Key Concepts:
@FunctionalInterface
interface Drawable {
};
Key Concepts:
Syntax: ContainingClass::staticMethodName
Syntax: containingObject::instanceMethodName
Syntax: ClassName::new
class MyClass {
interface MyInterface {
void display();
5. Stream API
names.stream()
.forEach(System.out::println);
// Output:
// Sourabh
// Swapnil
names.stream()
.sorted()
.forEach(System.out::println);
// Output:
// Aryan
// Mohit
// Ritvik
// Sourabh
// Swapnil
Key Concepts:
Example:
interface MyInterface {
@Override
obj.abstractMethod();
Key Concepts:
Purpose: To encode binary data (0s and 1s) into a textual format
(A-Z, a-z, 0-9, +, /) that can be easily transported over networks
without corruption or data loss. Decoding reverses this process.
import java.util.Base64;
// Encode
String encodedString =
Base64.getEncoder().encodeToString(originalInput.getBytes());
// Decode
8. For-Each Method
Key Concepts:
import java.util.ArrayList;
import java.util.List;
names.add("Alice");
names.add("Bob");
names.add("Charlie");
// Output:
// Hello, Alice
// Hello, Bob
// Hello, Charlie
9. Try-With-Resources
Introduced in Java 7, Try-With-Resources is a statement that ensures that
each resource opened in the try block is automatically closed at
the end of the block, whether normally or abnormally.
Key Concepts:
Syntax: Resources are declared within the parentheses after the try
keyword.
Syntax Example:
} catch (Exception e) {
// Handle exceptions
Benefits:
Key Concepts:
Metadata: Data about data (e.g., details of a photo like time, date,
size). Annotations provide metadata about classes, interfaces,
methods, fields, etc.
Introduced: JDK 5.
Introduced: Java 8.
Purpose: Can be applied to any place where a type is used, not just
declarations. This includes:
new expressions.
instanceof checks.
Type casts.
Example:
// ...
Introduced: Java 8.
Example:
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(Schedules.class)
@interface Schedule {
String day();
@Retention(RetentionPolicy.RUNTIME)
@interface Schedules {
Schedule[] value();
@Schedule(day = "Monday")
@Schedule(day = "Wednesday")
Key Concepts:
Example:module com.javamodule {
}
Benefits:Reliable Configuration: Strong encapsulation ensures
that modules only expose what they intend to.
Key Concepts:
Introduced: Java 7.
import java.util.ArrayList;
import java.util.List;
// Java 7 would throw a compilation error here for anonymous inner class
add(10);
add(20);
};
add(30);
add(40);
};
System.out.println(numbers);
System.out.println(numbers9);
}
Key Concepts:
Keyword: var
Compiler's Role: The Java compiler (JDK 10+) figures out the data
type of the variable at compile time based on the value assigned to
it.
Index variables in traditional for loops (e.g., for (var i = 0; i < 10; i+
+)).
Iteration variables in enhanced for loops (e.g., for (var item : list)).
Method parameters.
import java.util.ArrayList;
import java.util.List;
public class VarKeywordExample {
// Before Java 10
System.out.println(newName);
newMessages.add("Hello");
System.out.println(newMessages);
Key Concepts:
int numLettersOld;
switch (day) {
case "MONDAY":
case "FRIDAY":
case "SUNDAY":
numLettersOld = 6;
break;
case "TUESDAY":
numLettersOld = 7;
break;
case "THURSDAY":
case "SATURDAY":
numLettersOld = 8;
break;
case "WEDNESDAY":
numLettersOld = 9;
break;
default:
numLettersOld = -1;
};
Key Concepts:
System.out.println("It's a weekday!");
};
Key Concepts:
Example:
" <body>\n" +
" </body>\n" +
"</html>";
<html>
<body>
<p>Hello, world!</p>
</body>
</html>
""";
System.out.println("New HTML:\n" + htmlNew);
"name": "Alice",
"age": 30
""";
System.out.println("JSON:\n" + json);
Key Concepts:
Example:
// Output:
// Brand: Swift
Key Concepts:
Use Cases: Often used with Pattern Matching for instanceof (an
upcoming feature not covered in detail here but mentioned as a
future enhancement).
Example:
}
public non-sealed class Square extends Shape {
This briefing document covers the core concepts and important details of
the new Java features discussed in the provided source. Each section aims
to capture the essence of the topic, its purpose, syntax, and benefits,
along with relevant examples.
convert_to_textConvert to source
Quiz: Short Answer Questions
Answer each question in 2-3
sentences.
1. What is a Functional Interface in Java 8?
Answer Key