BAEL-4663: Whats new in Java 15

This commit is contained in:
Michael Pratt 2020-11-11 14:51:36 -07:00
parent f77d85f21e
commit 2715bde82a
4 changed files with 58 additions and 0 deletions

View File

@ -0,0 +1,16 @@
package whatsnew.records;
/**
* Java record with a header indicating 2 fields.
*/
public record Person(String name, int age) {
/**
* Public constructor that does some basic validation.
*/
public Person {
if (age < 0) {
throw new IllegalArgumentException("Age cannot be negative");
}
}
}

View File

@ -0,0 +1,13 @@
package whatsnew.sealedclasses;
import java.util.Date;
public non-sealed class Employee extends Person {
public Date getHiredDate()
{
return new Date();
}
}

View File

@ -0,0 +1,4 @@
package whatsnew.sealedclasses;
public final class Manager extends Person {
}

View File

@ -0,0 +1,25 @@
package whatsnew.sealedclasses;
import java.util.Date;
public sealed class Person permits Employee, Manager
{
/**
* Demonstration of pattern matching for instanceof
*
* @param person A Person object
* @return
*/
public static void patternMatchingDemo(Person person)
{
if(person instanceof Employee employee)
{
Date hiredDate = employee.getHiredDate();
}
if(person instanceof Employee employee && employee.getHiredDate() != null)
{
Date hiredDate = employee.getHiredDate();
}
}
}