{"id":27763,"date":"2020-03-16T16:53:32","date_gmt":"2020-03-16T16:53:32","guid":{"rendered":"https:\/\/blog.jetbrains.com\/idea\/?p=21404"},"modified":"2020-03-27T05:22:00","modified_gmt":"2020-03-27T05:22:00","slug":"java-14-and-intellij-idea","status":"publish","type":"idea","link":"https:\/\/blog.jetbrains.com\/ja\/idea\/2020\/03\/java-14-and-intellij-idea","title":{"rendered":"Java 14 and IntelliJ IDEA"},"content":{"rendered":"<p>Java 14 packs a lot of Java language features for you. It includes <a href=\"https:\/\/openjdk.java.net\/jeps\/359\" target=\"_blank\" rel=\"noopener\">Records<\/a> and <a href=\"https:\/\/openjdk.java.net\/jeps\/305\" target=\"_blank\" rel=\"noopener\">Pattern Matching for instanceof<\/a> as preview language features, and <a href=\"https:\/\/openjdk.java.net\/jeps\/368\" target=\"_blank\" rel=\"noopener\">Text Blocks<\/a> in the second preview. It also adds <a href=\"https:\/\/openjdk.java.net\/jeps\/361\" target=\"_blank\" rel=\"noopener\">Switch Expressions<\/a> as a Standard language Feature.<\/p>\n<p>With Records, you get a compact syntax for declaring data classes. By using just a single line of code, you can model your data with ease. Don\u2019t worry \u2013 the compiler does the heavy lifting to add the methods required to make the class work for you.<!--more--><\/p>\n<p>The usage of the <code>instanceof<\/code> operator is simplified with Pattern Matching for instanceof. After introducing a binding variable, you don\u2019t need additional variables or explicit casting, which also makes your code safe and concise to write and read.<\/p>\n<p>Released as a preview language feature in Java 13, Text Blocks add new escape sequences to improve the processing of whitespaces in multi-line string values. Switch Expressions have been added as a standard language feature in Java 14, with no changes from its previous version (it was introduced in Java 12 as a preview language feature).<\/p>\n<p>In this article, I will cover what records are, talk about pattern matching for instanceof, new features in Text Blocks, and explain how to use them all in IntelliJ IDEA. Let\u2019s get started!<\/p>\n<p><iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/2L0aGuH6K-Q\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<p><H1>Records<\/H1><\/p>\n<p>Records introduce a new type declaration in Java which simplifies the task of modeling your data as (shallowly immutable) data. Though it helps cut down significantly on the boilerplate code, this isn\u2019t the primary reason for its introduction.<\/p>\n<p>Here\u2019s an example:<\/p>\n<p><code>record Person(String name, int age) {}<\/code><\/p>\n<p>With just one line of code, the preceding example defines a record <code>Person<\/code> with two components <code>name<\/code> and <code>age<\/code>. To create a record using IntelliJ IDEA 2020.1, select \u2018Record (Preview Feature)\u2019 in the \u2018New Java Class\u2019 dialog box. Fill in the name and you are good to go.<\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-16062\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/03\/idea-Java14-record-create-cover.png\" alt=\"\" width=\"803\" data-gif-src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/03\/idea-Java14-record-create.gif\" \/><\/p>\n<p><H2>IntelliJ IDEA configuration<\/H2><\/p>\n<p>Java 14 features are supported in IntelliJ IDEA 2020.1, which will be released in April 2020. The exciting news is that you can participate in <a href=\"https:\/\/www.jetbrains.com\/idea\/nextversion\/\" target=\"_blank\" rel=\"noopener\">IntelliJ IDEA\u2019s EAP (Early Access Program)<\/a> to download its preview builds for free and try out the Java 14 features today.<\/p>\n<p>Configure IntelliJ IDEA 2020.1 to use JDK 14 for Project SDK and choose the Project language level as \u201814 (Preview) &#8211; Records, patterns, text blocks\u2019 for your Project and Modules settings.<\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-16062\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/03\/idea-Java14-ij-settings.png\" alt=\"\" width=\"803\" \/><\/p>\n<p>Starting with IntelliJ IDEA 2020.1, you can also download the JDK from within IntelliJ IDEA and configure it. All JDK versions from various providers might not be visible in this dialog box. This is due to the various rules that apply to them &#8211; you need to explicitly accept certain conditions before using them. In this case, please download the JDK from the provider&#8217;s website and then configure IntelliJ IDEA to use it. <\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-16062\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/03\/idea-Java14-settings-download-jdk-cover.png\" alt=\"\" width=\"803\" data-gif-src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/03\/idea-Java14-settings-download-jdk.gif\" \/><\/p>\n<p><H2>Implicit members added to a record<\/H2><\/p>\n<p>The compilation process creates a full-blown class, adding instance variables and methods to a record. We can open the Person.class file in IntelliJ IDEA to view its decompiled version (in the project window, scroll to folder \u2018out\u2019 and click on Person.class to open it in the editor window):<\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-16062\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/03\/idea-Java14-records-view-bytecode-cover2.png\" alt=\"\" width=\"803\" \/><\/p>\n<p>As you can see in the decompiled code for the record Person, the compiler (re)defines it as a final class, extending the <code>java.lang.Record<\/code> class from the core Java API. For each of the components of the record <code>Person<\/code>, the compiler defines a final instance variable (<code>name<\/code> and <code>age<\/code>). Interestingly, the name of the getter method is the same as that of the data variable (it doesn\u2019t start with &#8216;<code>get<\/code>&#8216; or &#8216;<code>is<\/code>&#8216;). Since a record is supposed to be immutable, no setter methods are defined.<\/p>\n<p>The methods <code>toString()<\/code>, <code>hashCode()<\/code>, and <code>equals()<\/code> are also generated automatically for records.<\/p>\n<p><H2>Using records<\/H2><\/p>\n<p>You can instantiate a record by using the operator <code>new<\/code>. The default constructor of a record accepts values for its components in the order of their definition. You can also call the implicit methods that are generated automatically for a record. Here\u2019s an example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"\">public class Java14 {\r\n   public static void main(String[] args) {\r\n       var shreya = new Person(&quot;Shreya&quot;, 20);\r\n       var harry = new Person(&quot;Harry&quot;, 45);\r\n\r\n       System.out.println(shreya);\r\n       System.out.println(shreya.equals(harry));\r\n       System.out.println(shreya.name());\r\n       System.out.println(shreya.age());\r\n       System.out.println(shreya.hashCode());\r\n   }\r\n}<\/pre>\n<p>Don\u2019t miss the parameter hints when you are passing values to the constructor in IntelliJ IDEA:<\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-16062\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/03\/idea-Java14-records-use-records-cover.png\" alt=\"\" width=\"803\" data-gif-src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/03\/idea-Java14-records-use2.gif\" \/><\/p>\n<p>As you can notice from the output, the <code>toString()<\/code> displays the name of the record, its components, and their values. Also, no setter methods are defined by default for the components of a record (since records are supposed to be immutable). If you try to do that yourself, you\u2019ll get a compilation error (because final variables can\u2019t be reassigned).<\/p>\n<p><H2>What you can and can\u2019t add to a record<\/H2><\/p>\n<p>You can add static fields, and instance or static methods to a record, if you need them:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"\">public record Person(String name, int age) {\r\n    Person {\r\n        instanceCtr++;\r\n    }\r\n    private static int instanceCtr;\r\n    static int getInstanceCtr() {\r\n    \treturn instanceCtr;\r\n    }\r\n}<\/pre>\n<p>However, since a record is a <code>final<\/code> class, you can\u2019t define it as an <code>abstract<\/code> class. Additionally, it can\u2019t extend another class (since it implicitly extends the <code>java.lang.Record<\/code> class). But there are no restrictions on its implementing interfaces. <\/p>\n<p>Even though you can add static fields to a record, you can\u2019t add instance variables to it. This is because a record is supposed to limit the instance members to the components it defines in the record definition.<\/p>\n<p>When you invoke inspection \u2018Generate\u2019 (Alt + Insert for Win and Linux\/ ^N for macOS) to generate code for insertion in a record, you\u2019ll notice that you don\u2019t get an option to generate setters. This is intentional because a record is supposed to be (shallowly) immutable.<\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-16062\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/03\/idea-Java14-records-only-getter.png\" alt=\"\" width=\"803\" \/><\/p>\n<p>A record has many similarities to the enums in Java \u2013 they could be considered siblings. On compilation, an enum gets multiple methods. It implicitly extends java.lang.Enum, and can\u2019t be extended externally. Like a record, an enum can also implement interfaces.<\/p>\n<p><H2>Modifying the default behavior of a constructor in a record<\/H2><\/p>\n<p>The default constructor of a record just initializes its state with the values you pass to it. You can change this default behavior to, say, validate the parameter values before moving forward with their assignment. <\/p>\n<p>IntelliJ IDEA lets you insert a compact, canonical, or custom constructor in a record. For your quick information, a compact constructor doesn\u2019t have a parameter list, not even parentheses. A canonical constructor is the one whose signature matches with the record&#8217;s state description. A custom constructor lets you choose the record components you want to pass to the constructor of a record. With all these constructors, you can add validation code. A compact constructor enables you to add code without the full boilerplate code. <\/p>\n<p>Let\u2019s see how you can insert a compact constructor using the Alt + Insert shortcut in IntelliJ IDEA, adding validation code to it. To verify this code, you can run the application, Java14, in several ways: the menu option \u2018Run\u2019, \u2018Find Action\u2019, or my preferred option \u2013 pressing Ctrl + Ctrl (works on all OS) and selecting the configuration to run:<\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-16062\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/03\/idea-Java14-records-construtors-cover.png\" alt=\"\" width=\"803\" data-gif-src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/03\/idea-Java14-records-construtors2.gif\" \/><\/p>\n<p>You can also add a canonical constructor to a record. This defines a parameter list \u2013 which must have the same names and order as those of the components of a record. A mismatch would result in a compilation error.<\/p>\n<p>By invoking context actions in IntelliJ IDEA (with Alt + Enter), you can easily convert a canonical constructor to a compact constructor, and vice versa:<\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-16062\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/03\/idea-Java14-records-convert-bet-constructors-cover.png\" alt=\"\" width=\"803\" data-gif-src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/03\/idea-Java14-records-convert-bet-constructors.gif\" \/><\/p>\n<p>While trying out a new feature, you might edit code multiple times, resulting in redundant code in your codebase. For example, after multiple edits to your compact or canonical constructor in a record, you might end up with a constructor that doesn\u2019t include any additional code, such as validation of parameters.<\/p>\n<p>If this happens, IntelliJ IDEA can detect such constructors and offer to delete them for you:<\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-16062\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/03\/idea-Java14-records-redundant-constructor-cover.png\" alt=\"\" width=\"803\" data-gif-src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/03\/idea-Java14-records-redundant-constructor.gif\" \/><\/p>\n<p><H2>Defining a Generic record<\/H2><\/p>\n<p>You can define records with generics. Here\u2019s an example of a record called <code>Parcel<\/code>, which can store any object as its <code>contents<\/code>, and capture the parcel\u2019s dimensions and weight:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"\">public record Parcel&lt;T&gt;(T contents,\r\n\tdouble length,\r\n\tdouble breadth,\r\n\tdouble height,\r\n\tdouble weight) {}<\/pre>\n<p>You can instantiate this record as follows:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"\">class Table{ \/* class code *\/ }\r\npublic class Java14 {\r\n    public static void main(String[] args) {\r\n    \tParcel&lt;Table&gt; parcel = new Parcel&lt;&gt;(new Table(), 200, 100, 55, 136.88);\r\n    \tSystem.out.println(parcel);\r\n    }\r\n}<\/pre>\n<p><H2>Adding annotations to record components<\/H2><\/p>\n<p>You can add an appropriate annotation to the components of a record. For example, for the record Person defined earlier in this post, you can add a @NotNull annotation to its component name. This also adds the annotation to its (canonical) constructor:<\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-16062\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/03\/idea-Java14-records-add-annotations-cover.png\" alt=\"\" width=\"803\" data-gif-src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/03\/idea-Java14-records-add-annotations.gif\" \/><\/p>\n<p><H2>Reading and Writing Records to a File<\/H2><\/p>\n<p>Writing a record to a file and reading it using the Java File I\/O API is simple. Let your record implement the <code>Serializable<\/code> interface and you are good to go. Here\u2019s the modified version of the record <code>Person<\/code>, which you can write to and read from a file:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"\">package com.jetbrains;\r\npublic record Person(String name, int age) implements Serializable {}\r\npublic class Java14 {\r\n    public static void main(String[] args) {\r\n    \tPerson javaDev = new Person(&quot;Java Dev&quot;, 25);\r\n    \twriteToFile(javaDev, &quot;Java14-records&quot;);\r\n        System.out.println(readFromFile(&quot;Java14-records&quot;));\r\n    }\r\n    static void writeToFile(Person obj, String path) {\r\n    \ttry (ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream(path))){\r\n        \toos.writeObject(obj);\r\n    \t} catch (IOException e) {\r\n        \te.printStackTrace();\r\n        }\r\n    }\r\n    static Person readFromFile(String path) {\r\n    \tPerson result = null;\r\n    \ttry (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(path))){\r\n        \tresult = (Person) ois.readObject();\r\n    \t} catch (ClassNotFoundException | IOException e) {\r\n        \te.printStackTrace();\r\n    \t}\r\n    \treturn result;\r\n    }\r\n}<\/pre>\n<p><H2>Refactoring the signature of a Record<\/H2><\/p>\n<p>You can refactor a Record and modify the order of its components or types, modify their names, and add new or remove existing ones. IntelliJ IDEA 2020.1 introduces a simplified approach to apply <a href=\"https:\/\/blog.jetbrains.com\/idea\/2020\/02\/intellij-idea-2020-1-eap4\/\">Rename or Change Signature Refactorings<\/a>. The changes would reflect in a record\u2019s canonical constructor and its instance creation:<\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-16062\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/03\/idea-Java14-records-refactor-signature3-cover.png\" alt=\"\" width=\"803\" data-gif-src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/03\/idea-Java14-records-refactor-signature3.gif\" \/><\/p>\n<p><H2>A restricted identifier<\/H2><\/p>\n<p>&#8216;<code>record<\/code>&#8216; is a restricted identifier (like &#8216;<code>var<\/code>&#8216;) and isn\u2019t a regular keyword (yet). So, the following code is valid:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"\">int record = 10;\r\nvoid record() {}<\/pre>\n<p>However, you may want to refrain from using <code>record<\/code> as an identifier because such code becomes confusing as developers start using records.<\/p>\n<p><H1>Pattern Matching for instanceof<\/H1><\/p>\n<p>Many Java developers use the <code>instanceof<\/code> operator to check whether a given reference variable is of a certain type. They compare a reference variable to a type by using the <code>instanceof<\/code> operator. If the result is <code>true<\/code>, the next obvious step is to explicitly cast it to the type they compared it with to access its members. These steps have an obvious repetition here, like <em>compare-ifResultTrue-cast<\/em>.<\/p>\n<p>Here\u2019s an example of code that can be commonly found in code bases:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"\">void outputValueInUppercase(Object obj) {\r\n    if (obj instanceof String) {               \r\n    \tString s = (String) obj;          \t\r\n    \tSystem.out.println(s.toUpperCase());  \r\n    }\r\n}<\/pre>\n<p>Pattern Matching for instanceof removes this redundant code by introducing a pattern variable with the <code>instanceof<\/code> operator. If the <code>instanceof<\/code> condition is true, the pattern variable binds to the variable being compared, which prevents the need to define an additional variable or for explicit casting to use its members.<\/p>\n<p>In IntelliJ IDEA 2020.1, you can invoke context-sensitive actions on the variable <code>s<\/code> (by using Alt + Enter or by clicking the yellow light bulb) and select \u2018Replace \u2018<code>s<\/code>\u2019 with pattern variable\u2019 to use pattern matching in Java 14:<\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-16062\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/03\/idea-Java14-instanceof-firstconversion-cover.png\" alt=\"\" width=\"803\" data-gif-src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/03\/idea-Java14-instanceof-firstconversion.gif\" \/><\/p>\n<p>This modification introduces the pattern variable <code>s<\/code> (on line 5), which appears right after type <code>String<\/code>. This saves you from either defining a new variable or explicitly casting it to String before you could call the method <code>toUpperCase()<\/code> on it.<\/p>\n<p>Pattern variables are final local variables that are declared and defined at the same place. With other local variables, it is possible to declare them and defer their assignment. You can\u2019t assign another value to a pattern variable since it is implicitly final.<\/p>\n<p><a href=\"https:\/\/www.jetbrains.com\/idea\/nextversion\/\" target=\"_blank\" rel=\"noopener\">IntelliJ IDEA 2020.1<\/a> supports all the new features of Java 14, including Pattern Matching. Right now, you can participate in its Early Access Program (EAP) and download the Ultimate Edition to preview all the new features \u2013 for free.<\/p>\n<p><H2>Scope of Pattern variable<\/H2><\/p>\n<p>The scope of the pattern variable is limited. If you try to access it in the <code>else<\/code> block, you\u2019ll receive an error.<\/p>\n<p>Though it might be confusing, if the class <code>PatternMatching<\/code> defines an instance or static variable with the same name as the pattern variable (\u2018<code>s<\/code>\u2019), the preceding code will compile. In this case, \u2018<code>s<\/code>\u2019 in the <code>else<\/code> block would not refer to the pattern variable introduced in the <code>if<\/code> block:<\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-16062\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/03\/idea-Java14-instanceof-scope-cover.png\" alt=\"\" width=\"803\" data-gif-src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/03\/idea-Java14-instanceof-scope.gif\" \/><\/p>\n<p><H2>Simplifying conditional expressions<\/H2><\/p>\n<p>The simplicity of Pattern Matching might be deceptive. Here\u2019s an example of how developers usually override the <code>equals()<\/code> method for a class, in this case, <code>Monitor<\/code>, with two instance variables \u2013 <code>model<\/code> (String value) and <code>price<\/code> (double value):<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"\">public class Monitor {\r\n   String model;\r\n   double price;\r\n\r\n   @Override\r\n   public boolean equals(Object o) {\r\n       if (o instanceof Monitor) {\r\n           Monitor other = (Monitor) o;\r\n           if (model.equals(other.model) &amp;&amp; price == other.price) {\r\n               return true;\r\n           }\r\n       }\r\n       return false;\r\n   }\r\n}<\/pre>\n<p>This is how this <code>equals()<\/code> method could be simplified by using pattern matching for instanceof and the further simplification of <code>if<\/code> statements:<\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-16062\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/03\/idea-Java14-instanceof-equals-cover.png\" alt=\"\" width=\"803\" data-gif-src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/03\/idea-Java14-instanceof-equals.gif\" \/><\/p>\n<p><H2>Pattern Matching and out-of-the-box support for existing intentions in IntelliJ IDEA<\/H2><\/p>\n<p>Pattern Matching with instanceof can be used at multiple places to simplify your code. Various existing intentions work out of the box with Pattern Matching in IntelliJ IDEA, such as inverting, merging, splitting, simplifying if statements, removing redundant else blocks, simplifying conditional expressions, and many more. <\/p>\n<p>The following code removes redundant casting by using Pattern Matching for instanceof and then simplifies its <code>if<\/code> statements:<\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-16062\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/03\/idea-Java14-instanceof-demo2-cover.png\" alt=\"\" width=\"803\" data-gif-src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/03\/idea-Java14-instanceof-demo2.gif\" \/><\/p>\n<p>The real strength of Pattern Matching for instanceof lies in how it can be used with various other intentions. For example, the following code merges if statements, introduces a pattern variable, and inlines a variable:<\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-16062\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/03\/idea-Java14-instanceof-demo3-cover.png\" alt=\"\" width=\"803\" data-gif-src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/03\/idea-Java14-instanceof-demo3.gif\" \/><\/p>\n<p><H2>Simplifying usages of multiple instanceof in a code block<\/H2><\/p>\n<p>To look for places where you can use Pattern Matching for instanceof, spot usages of the <code>instanceof<\/code> operator and explicit casting of variables. For instance, the following example has multiple occurrences of the <code>instanceof<\/code> operator with explicit casting. Let\u2019s simplify it:<\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-16062\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/03\/idea-Java14-instanceof-demo4-cover.png\" alt=\"\" width=\"803\" data-gif-src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/03\/idea-Java14-instanceof-demo4-ver2.gif\" \/><\/p>\n<p><H2>Inlining of pattern variables<\/H2><\/p>\n<p>If you don&#8217;t need the pattern variables, you can inline them with \u2018Inline pattern variable\u2019  refactoring by using the shortcut Ctrl+Alt+N on Win and Linux\/ \u2325\u2318N on macOS. After you do so, all its usages would be replaced with explicit casting.<\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-16062\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/03\/idea-Java14-instanceof-inline-pattern-variable-cover.png\" alt=\"\" width=\"803\" data-gif-src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/03\/idea-Java14-instanceof-inline-pattern-variable.gif\" \/><\/p>\n<p><H2>Automatic pattern introduction on &#8216;extract variable&#8217; refactoring<\/H2><\/p>\n<p>If you extract a variable from a cast expression, and IntelliJ IDEA detects that there were <code>instanceof<\/code> check before, then instead of creating a new local variable it just converts the instanceof to the pattern.<\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-16062\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/03\/idea-Java14-instanceof-extract-cast-variable-cover.png\" alt=\"\" width=\"803\" data-gif-src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/03\/idea-Java14-instanceof-extract-cast-variable.gif\" \/><\/p>\n<p><H1>Text Blocks <\/H1><\/p>\n<p>The Text Blocks feature is in its second preview in Java 14. It introduces two new escape sequences, <code>\\<\/code> and <code>\\s<\/code>, to exercise finer control of how new-line characters and end-of-line spaces are handled in text blocks (multiline strings).<\/p>\n<p>I\u2019m only going to cover how Text Blocks have changed in Java 14 in this blog. For the complete coverage on what Text Blocks are and how IntelliJ IDEA supports them, please refer to our previous blog post on <a href=\"https:\/\/blog.jetbrains.com\/idea\/2019\/11\/java-13-and-intellij-idea\/\" rel=\"noopener\" target=\"_blank\">Java 13 and IntelliJ IDEA<\/a>.<\/p>\n<p>Multi-line string values in text blocks include newline characters, which can be suppressed by using <code>\\<\/code> at the end of a line. Also, the spaces at the end of a line in text blocks are removed by default. To insert them, you can use the escape sequence <code>\\s<\/code>:<\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-16062\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/03\/idea-Java14-text-blocks-cover.png\" alt=\"\" width=\"803\" data-gif-src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/03\/idea-Java14-text-blocks.gif\" \/><\/p>\n<p><H1>Switch Expressions<\/H1><\/p>\n<p>Switch Expressions were first released as a preview language feature with Java 12. After being released in the second preview in Java 13, Switch expressions are being added as a standard feature in Java 14.<\/p>\n<p>There have been no changes in Switch Expressions since its last release in Java 13. For details on how it is supported in IntelliJ IDEA, please see our <a href=\"https:\/\/blog.jetbrains.com\/idea\/2019\/11\/java-13-and-intellij-idea\/\" rel=\"noopener\" target=\"_blank\">previous blog post<\/a>.<\/p>\n<p><H1>Preview Language Features<\/H1><\/p>\n<p>Records and Pattern Matching with instanceof have been released as a preview language feature in Java 14. With Java\u2019s new release cadence of six months, new language features are released as preview features. They are complete but not permanent. A preview language feature essentially means that this feature is ready to be used by developers, although its finer details could change in a future Java release depending on the developers\u2019 feedback. Unlike an API, language features can\u2019t be deprecated in the future. So, if you have any feedback on text blocks, feel free to share it on the <a href=\"https:\/\/mail.openjdk.java.net\/mailman\/listinfo\/amber-dev\" rel=\"noopener\" target=\"_blank\">JDK mailing list<\/a> (free registration required).<\/p>\n<p>IntelliJ IDEA is committed to only supporting preview features for the current JDK. This is due to how these features work. Preview language features can change across Java versions, until they are dropped or added as a standard language feature. Code which uses a preview language feature from an older release of the Java SE Platform might not compile or run on a newer release. For example, Switch Expressions in Java 12 was released with usage of <code>break<\/code> to return value from its branch, which was later changed to <code>yield<\/code>. Support for using <code>break<\/code> to return a value from Switch Expressions is already dropped in IntelliJ IDEA.<\/p>\n<p><H1>Looking ahead<\/H1><\/p>\n<p>Work is in progress to derive deconstruction patterns for records. It should present interesting use cases with \u2018Sealed Types\u2019 and \u2018Pattern Matching\u2019. A future version of Java should also see the addition of methods to the Object class to enable reflections to work with Records. In a future version of Java, you might soon be able to use Pattern Matching with Switch Expression.<\/p>\n<p>Java is moving forward fast, and IntelliJ IDEA 2020.1 supports all the new language features in Java 14. Try out Records, Pattern Matching for instanceof, Text Blocks, and Switch Expressions today. <\/p>\n<p>We love to hear from our users. Don\u2019t forget to submit your feedback on support of these features in IntelliJ IDEA. <\/p>\n<p>Happy Developing!<\/p>\n","protected":false},"author":921,"featured_media":0,"comment_status":"open","ping_status":"open","template":"","categories":[826,808,89,907],"tags":[3374,155,3388,3390,3389,3391,3372],"cross-post-tag":[],"acf":[],"_links":{"self":[{"href":"https:\/\/blog.jetbrains.com\/ja\/wp-json\/wp\/v2\/idea\/27763"}],"collection":[{"href":"https:\/\/blog.jetbrains.com\/ja\/wp-json\/wp\/v2\/idea"}],"about":[{"href":"https:\/\/blog.jetbrains.com\/ja\/wp-json\/wp\/v2\/types\/idea"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/ja\/wp-json\/wp\/v2\/users\/921"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/ja\/wp-json\/wp\/v2\/comments?post=27763"}],"version-history":[{"count":0,"href":"https:\/\/blog.jetbrains.com\/ja\/wp-json\/wp\/v2\/idea\/27763\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.jetbrains.com\/ja\/wp-json\/wp\/v2\/media?parent=27763"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/ja\/wp-json\/wp\/v2\/categories?post=27763"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/ja\/wp-json\/wp\/v2\/tags?post=27763"},{"taxonomy":"cross-post-tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/ja\/wp-json\/wp\/v2\/cross-post-tag?post=27763"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}