Merge pull request #10245 from michael-pratt/BAEL-4663
BAEL-4663: Whats new in Java 15
This commit is contained in:
commit
350f093108
@ -51,8 +51,8 @@
|
|||||||
<configuration>
|
<configuration>
|
||||||
<release>${maven.compiler.release}</release>
|
<release>${maven.compiler.release}</release>
|
||||||
<compilerArgs>--enable-preview</compilerArgs>
|
<compilerArgs>--enable-preview</compilerArgs>
|
||||||
<source>15</source>
|
<source>14</source>
|
||||||
<target>15</target>
|
<target>14</target>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.baeldung.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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package com.baeldung.whatsnew.sealedclasses;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
public non-sealed class Employee extends Person {
|
||||||
|
public Date getHiredDate() {
|
||||||
|
return new Date();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,4 @@
|
|||||||
|
package com.baeldung.whatsnew.sealedclasses;
|
||||||
|
|
||||||
|
public final class Manager extends Person {
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
package com.baeldung.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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user