-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathJavaNewFeature.java
More file actions
37 lines (31 loc) · 1.49 KB
/
JavaNewFeature.java
File metadata and controls
37 lines (31 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
public class NewJavaFeaturesDemo {
// Record - a concise way to define immutable data carriers
record Vehicle(String model, String type, double price) {}
public static void main(String[] args) {
// Example of Text Blocks (introduced in Java 13)
String welcomeMessage = """
================================
Welcome to the Vehicle Info System
================================
""";
System.out.println(welcomeMessage);
// Creating an instance of Vehicle using a Record
Vehicle car = new Vehicle("Tesla Model 3", "Electric", 49999.99);
// Displaying vehicle details
printVehicleInfo(car);
// Pattern Matching for instanceof (introduced in Java 16)
Object obj = car;
if (obj instanceof Vehicle vehicle) {
// No need to cast obj to Vehicle, it's done automatically
System.out.println("\nUsing Pattern Matching for instanceof:");
System.out.printf("This is a %s (%s) priced at $%.2f\n", vehicle.model(), vehicle.type(), vehicle.price());
} else {
System.out.println("The object is not a Vehicle");
}
}
// Method to print Vehicle details using the new "record" feature
public static void printVehicleInfo(Vehicle vehicle) {
System.out.println("Vehicle Information:");
System.out.printf("Model: %s\nType: %s\nPrice: $%.2f\n", vehicle.model(), vehicle.type(), vehicle.price());
}
}