- A new
varkeyword was added that allows local variables to be declared in a more concise manner, - Ex:
var name = new String("hello"); System.out.println(name); // hello var student = new Student(121,"Doniel",5.9); System.out.println(student.getName()); // Doniel System.out.println(student.getHeight()); // 5.9
- Cleaner and less error-prone syntax,
caselabel followed by->followed by one of the following:An expression, including but not limited to a constant value,- A
throwstatement, - A
code blockusing opening and closing curly brackets
- Ex:
int i = 20; switch (i){ case 10 -> { System.out.println(10); System.out.println("10"); } case 20-> System.out.println(20); // executed default-> System.out.println("Other"); } i = 40; String message = switch (i){ case 10 -> "10"; case 20 -> "20"; case 30 -> throw new RuntimeException("idk"); case 40 ->{ System.out.println("40"); // executed yield "like return"; } default -> "none"; }; System.out.println(message); // like return
- Till now, we do like this
Student student = new PartTimeStudent(12,"Imran",5.5,12); PartTimeStudent partTimeStudent; if(student instanceof PartTimeStudent){ partTimeStudent = (PartTimeStudent)student; }
- Above can be simplified like this:
Student student = new PartTimeStudent(12,"Imran",5.5,12); if(student instanceof PartTimeStudent partTimeStudent){ // partTimeStudent }
- Another way to
disable inheritanceintroduced inJava 17, - Allows you to define a class and exactly
what classes can subclass it, - Subclass of Sealed class must define their status:
final,sealed, ornon-sealed, - Ex(Sealed class):
Inheriting
public abstract sealed class Security permits Lock, Pin, Password { }
sealed-class:Inheringfinal class Lock extends Security{ } // no class can inherit it non-sealed class Pin extends Security{ } // any class can inherit it sealed class Password extends Security permits MyPassword{ } // same as sealed
subclassofsealed-class,final class MyPassword extends Password{} // inherited above sealed class class MyPin extends Pin{} // Since Pin class can be inherited by any class
- Is done via
TextBlocks, - A
text blockmust start- with
three quotation marks, then a new line, then- your text as you write normally,
- with
- Ex(Earlier way):
New way:
String formattedTextEarlier = "This is first line.\n"+ "THis is second line.\n"+ "Continue like that"; System.out.println(formattedTextEarlier);
Output:String formattedText = """ This is first line. THis is second line. Continue like that"""; System.out.println(formattedText);
Below one isThis is first line. THis is second line. Continue like thatinvalidsince uou can't write just after first""",String formattedText2 = """This is first line. THis is second line. Continue like that""";
- Previous way:
New way:
public class Person { final String name; final int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } }
Using like this:public record PersonRecord(String name, int age) { }
Person student = new Person("Saeed",2345); PersonRecord record = new PersonRecord("Saeed",2345); System.out.println(student.getName() + " - " + record.name()); // Saeed - Saeed System.out.println(student.getAge() + " - " + record.age()); // 2345 - 2345