Java 16
Get up to speed on Java 16 with our top tips.
IntelliJ IDEA always supports the most recent version of Java. If you want to see the new features that are most interesting to Java developers, and see how IntelliJ IDEA helps us to use these features, these tips are a great place to start.
Create Record
Create a new Java record class
Create a new record by pressing ⌘N (macOS) / Alt+Insert (Windows/Linux) on the Project Window. From the New Java Class dialog, type the name of the record and select the Record type.
Convert class to record
Use inspections to convert classes to Java 16 records
IntelliJ IDEA can identify classes that are data holders and can be converted to records. The class will be highlighted with a warning (yellow underline) - press ⌥⏎ (macOS) / Alt+Enter (Windows/Linux) and IntelliJ IDEA will suggest converting the class to a record.
The fields will be become record components, the constructor and accessors will be removed. If we had equals, hashCode and toString methods on our class, we can optionally remove these and rely on the default record implementation.
Convert from Record
Use quick intentions to convert a record to a class
If we decide that maybe a record should be a full Java class, we can get IntelliJ IDEA to automatically convert a record into a Java class.
Press ⌥⏎ (macOS) / Alt+Enter (Windows/Linux) on the record name, and IntelliJ IDEA offers a suggestion to convert the record to a class.
Use Pattern Matching for instanceof
Inspections can guide us to use new Java features
If our code uses an instanceof followed by a cast, IntelliJ IDEA highlights these with a warning and suggests replacing this with pattern matching for instanceof. Note that this is only available if you're using a language level of Java 16 or above.
Press ⌥⏎ (macOS) / Alt+Enter (Windows/Linux) and choose Replace with pattern variable.
public class PatternMatchingSwitch {
void outputValueInUpperCase(Object obj) {
if (obj instanceof String) {
String s = (String) obj;
System.out.println(s);
}
}
}
You can press ⌥⏎ (macOS) / Alt+Enter (Windows/Linux) and choose Replace 's' with pattern variable.
public class PatternMatchingSwitch {
void outputValueInUpperCase(Object obj) {
if (obj instanceof String s) {
System.out.println(s);
}
}
}
Identify and fix deprecated calls
IntelliJ IDEA will warn you if you're using deprecated methods, and they will be shown with a strikethrough if they're marked for removal
IntelliJ IDEA shows deprecated methods that are marked for removal with a red strikethrough. JDK 16 has marked the wrapper class constructors for removal, so you'll see these with a red strikethrough in IntelliJ IDEA.
Use new methods on the Streams API
Inspections can show us new features from Java 16
Java 16 introduced some new methods on the Streams API. IntelliJ IDEA shows us places that can use the new methods and can automatically convert the code to use these new methods.
The code will be highlighted in yellow as a warning. Press ⌥⏎ (macOS) / Alt+Enter (Windows/Linux) and choose "Replace collect(toUnmodifiableList()) with toList()".
Turn on Preview Features to try new Java features
The latest versions of Java contain "Preview Features", features that are fully functional but require developer feedback. Try them out in IntelliJ IDEA.
If you want to use preview features in the latest versions of Java in IntelliJ IDEA, you need to set the language level to Preview.
Go to Project Structure, ⌘; (macOS) / Ctrl+Alt+Shift+S (Windows/Linux), make sure you have the correct JDK selected, and pick the "Preview" option from the Language Level dropdown.
Create a correct parent Sealed Class
Sealed types are a new idea for Java developers, let IntelliJ IDEA guide you in how to use them
Sealed classes (a preview feature in Java 16) are a way to take control of the inheritance hierarchy of our classes. If we have a sealed class like this one, IntelliJ IDEA will tell us we need to "permit" the child class so it can extend this parent. It will also automatically make changes if it can.
Experiment with Sealed Types, use Alt+Enter (Windows/Linux) to see and accept suggestions.
Create a correct child Sealed Class
Sealed types are a new idea for Java developers, let IntelliJ IDEA guide you in how to create child classes
Sealed classes (first preview feature in Java 16) are a way to take control of the inheritance hierarchy of our classes. IntelliJ IDEA can let us know about problems in the classes that extend sealed classes, and suggest solutions.
Experiment with Sealed Types, use Alt+Enter (Windows/Linux) to see and accept suggestions.