Professional Resume Template for Software Developers
Java 14
1. Features in Java 14 (JDK 14)
Mutlu Okuducu/ Software Developer
Oracle has been announcement Java 14 (JDK 14) open-source released on 17 March 2020 offers for all developers and enterprises. JAVA
14 addresses a total of 16 main enhancements/changes.
The list of most important features to Java 14 for developers.
JEP 305 — Pattern Matching for instanceof
2. JEP 358 — NullPointerExceptions
JEP 359 — Records
JEP 361 — Switch Expressions
JEP 368 — Text Blocks (Second Preview)
For all features https://openjdk.java.net/projects/jdk/14/
JEP 305 — Pattern Matching for instanceof
We use instanceof-and-cast to check the object’s type and cast to a variable.
Before Java14
Java 14, we can refactor above code cast and bind like this:
Following example restriction
if (obj instanceof String) { // if true instanceof then
String t= (String) obj; // cast
System.out.println("String: " + t);
}
if (obj instanceof String t) { // instanceof, cast and bind variable in one line.
System.out.println("String: " + t);
}
3. You can find the code, instanceofs.
JEP 358 — Helpful NullPointerExceptions
NullPointerExceptions cause frustrations because they often appear in application logs when code is running in a production
environment, which can make debugging difficult. After all, the original code is not readily available. For example, consider the code
below:
Before Java 14, you might get the following error:
Exception in thread “main” java.lang.NullPointerException
com.java14features.nullpointerexception.Example1.main(Example1.java:15)
Unfortunately, if at line 15, there’s an assignment with multiple method invocations getAmount() and getCurrency() either one could be
returning null. So, it’s not clear what is causing the NullPointerException
//Legal use
if (obj instanceof String t && t.length() > 4) {
System.out.println("String: " + t);
}
//Cannot resolve symbol 't'
if(obj instanceof String t || t.length()>4){
System.out.println("String: "+t);
}
Account account =new Account();
String accountNumber=account.getAmount().getCurrency(); //line 15
System.out.println(accountNumber.length());
4. Now, with Java 14, there’s a new JVM feature through which you can receive more-informative diagnostics:
Exception in thread “main” java.lang.NullPointerException: Cannot invoke “com.java14features.dto.Amount.getCurrency()”
because the return value of “com.java14features.dto.Account.getAmount()” is null at
com.java14features.nullpointerexception.Example1.main(Example1.java:16)
The message now has two clear components:
The consequence: Amount.getCurrency() cannot be invoked.
The reason: The return value of Account.getAmount() is null.
The enhanced diagnostics work only when you run Java with the following flag: -XX:+ShowCodeDetailsInExceptionMessages
You can find the code, null pointer exception.
JEP 359 — Records
Records focus on certain domain classes whose purpose is only to store data in fields. A new class called record , it is a final class, not
abstract, and all of its fields are final as well. The record will automatically generate
the constructors , getters , equals() , hashCode() , toString() during compile-time and it can implements interfaces.
Restrictions:
A record is a final class
A record cannot extend a class
The value (reference) of a field is final and cannot be changed
A record class is an immutable
5. A record can not setters
You can find the code, records.
JEP 361 — Switch Expressions
The Switch Expressions is a preview feature in Java 12 and 13, it becomes a standard language feature, it means from Java 14 and
onward, we can use these Switch Expressions which can be used as an expression with help of arrow (->) , and now
can yield/return the value.
public record Transaction(
int id, double amount, String description {
}
/*
---- You can use JShell with the flag --enable-preview
Automatically generate:
* constructors
* getters
* equals()
* hashCode()
* toString()
*/
VehicleType vehicleType = VehicleType.AUTOMOBILE;
int speedLimit = switch (vehicleType) {
case BIKE, SCOOTER -> 40;
case MOTORBIKE, AUTOMOBILE -> 140;
case TRUCK -> 80;
default -> throw new IllegalStateException("No case found for: " + vehicleType);
};
System.out.println("Speed limit: " + speedLimit);
6. Following examples call method and return a new yield.
/////////////////////////////////////////
VehicleType vehicleType = VehicleType.TRUCK;
int speedLimit = switch (vehicleType) {
case BIKE, SCOOTER -> 40;
case MOTORBIKE, AUTOMOBILE -> 140;
case TRUCK -> {
int randomSpeed = ThreadLocalRandom.current().nextInt(70, 80);
yield randomSpeed;
}
};
System.out.println("Speed limit: " + speedLimit);
/////////////////////////////////////////
public static void main(String[] args) {
System.out.println( grade(9));
}
public static String grade(int mark) {
return switch (mark) {
case 9 -> "A";
case 8 -> "B";
case 7 -> "C";
case 6 -> "D";
default -> "F";
};
}
public static void main(String[] args) {
for (VehicleType vehicle : VehicleType.values()) {
int speedLimit = getSpeedLimit(vehicle);
System.out.println("Speed limit: " + vehicle + ":" + speedLimit);
7. You can find the code, switches.
JEP 368 — Text Blocks (Second Preview)
In Java, to embed HTML, XML, SQL, TEXT BLOCK or JSON snippet into code is often hard to read and hard to keep, and to
overcome this problem, Java 14 has introduced Text Block. A text block contains zero or more content characters, which are enclosed by
open and close delimiters.
}
}
private static int getSpeedLimit(VehicleType vehicleType) {
return switch (vehicleType) {
case BIKE, SCOOTER -> 40;
case MOTORBIKE, AUTOMOBILE -> 140;
case TRUCK -> 80;
};
}
/////////////////////////////////////////
public static void main(String[] args) {
Days day = FRIDAY;
int j = switch (day) {
case MONDAY -> 0;
case TUESDAY -> 1;
default -> {
int result = day.toString().length();
yield result;
}
};
System.out.println(j);
}
}
8. Text block using three double-quote characters
newlines (line-terminator) denoted by
for white space (single space) denoted by /s
string format for a parameter using %s
String text = """"""; // illegal text block start
String text = """ """; // illegal text block start
String text = """
"""; // Text block
//Without Text Block HTML
String html = "<html>n" +
" <body>n" +
" <p>Hello, world</p>n" +
" </body>n" +
"</html>n";
System.out.println(html);
// Text Block HTML
String htmlTag = """
<html>
<body>
<p>Hello, world</p>
</body>
</html>
""";
// Text Block multi line (paragraph)
String text = """
two escape sequences first is for newlines
and, second is to signify white space
or single space.
""";
9. Following examples are string formatted.
// Text Block SQL Query
String query1 = """
SELECT `EMP_ID`, `LAST_NAME` FROM `EMPLOYEE_TB`
WHERE `CITY` = 'INDIANAPOLIS'
ORDER BY `EMP_ID`, `LAST_NAME`;
""";
// Text Block JSON Example
String json= """
{
"amount": {
"currency": "string",
"value": 0
},
"creditorAccount": {
"accountName": "string",
"accountNumber": "string",
"sortCode": "string"
},
"debtorAccount": {
"accountName": "string",
"accountNumber": "string",
"sortCode": "string"
},
"paymentDate": "string",
"reference": "string"
}
""";
String html1 = """
<html>
<body>
<p>%s, %s</p>
</body>