Chapter 13
Chapter 13
Chapter-13
Type Annotations
Repeating Annotations
With Java 8 and later versions, you can use Repeating Annotations to apply the same
annotation multiple times to a single element, enhancing your code's flexibility. Although
Repeating Annotations aren't directly linked to the Stream API, they can be effectively used
Object Oriented Programming With JAVA
on classes, methods, or parameters that work with streams, boosting your Java programming
capabilities. Java allows you to repeat an annotation on the same declaration:
Syntax:
@Schedule(day = "Monday")
@Schedule(day = "Friday")
public void streamTask() {
// do something with streams
}
To effectively work with repeatable annotations, you'll need two components:
1. Container Annotation: This serves as a holder for the repeated annotations.
2. Main Annotation with @Repeatable: This is the primary annotation that can be used
multiple times. By having both of these elements, you can efficiently utilize repeatable
annotations in your code.
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.List;
import java.util.Arrays;
module-info.java
module mod.stream.app {
requires java.base; // optional (java.base is required by default)
}
Main.java
package com.example.streamapp;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> names = List.of("Ram", "Shyam", "Deepak");
names.stream().filter(name -> name.startsWith("D"))
.map(String::toUpperCase).forEach(System.out::println);
}
}
Switch Expressions
In Java 14, the traditional switch statement has been upgraded to a more robust
switch expression. a switch expression evaluates a single variable or expression and matches
it against multiple possible values. When a match is found, the corresponding code block is
executed. Unlike the conventional switch statements, switch expressions can return values,
making them more functional and versatile.
Object Oriented Programming With JAVA
This new feature offers improved conciseness and readability while allowing you to return
values directly from a switch, making it more efficient and powerful.
Example:
public class MyProg {
public static void main(String[] args) {
String json = """
{
"name": "Ram",
"age": 20,
"city": "Delhi"
}
""";
System.out.println(json);
}
}
Object Oriented Programming With JAVA
Records in Java
Records (introduced in Java 14 as a preview and finalized in Java 16) are a special
class type designed for immutable data-holding objects with minimal boilerplate. There are
several reasons to use it.
1. Automatic Getters-Java Records provide automatic getter methods, eliminating
the need to manually write methods like getX(). This feature streamlines your code and
improves its readability.
Sealed Classes
Sealed classes (introduced in Java 15 as a preview and finalized in Java 17) allow you
to restrict which classes can extend or implement them. Sealed classes allow you to specify
Object Oriented Programming With JAVA
which classes are permitted to extend or implement them. By using this feature, developers
can reduce the risk of unintended subclassing, making the code easier to manage and
understand.
To create a sealed class in Java, use the sealed modifier in your class declaration,
followed by the permits keyword to specify the subclasses. Here’s a simple example:
public class MyProg {
public static void main(String[] args) {
Shape s1 = new Circle(5);
Shape s2 = new Rectangle(4, 3);
System.out.println("Circle Area: " + s1.area());
System.out.println("Rectangle Area: " + s2.area());
}
}
public abstract sealed class Shape permits Circle, Rectangle {
public abstract double area();
}
final class Circle extends Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double area() {
return Math.PI * radius * radius;
}
}
final class Rectangle extends Shape {
private double length, width;
public Rectangle(double l, double w) {
this.length = l;
this.width = w;
}
@Override
public double area() {
return length * width;
}
}
In this example, only the Car and Truck classes are permitted to extend the Vehicle
class, providing a clear, predefined hierarchy.
Keyword Meaning
sealed Limits subclasses to only those in permits.
final No further subclassing allowed.
non-sealed Allows further subclassing beyond those in permits.