Note: many functions in this article are not new functions of jdk17, but describe my feelings about the new features brought by my programming during the transition from jdk8 to jdk17~
1. Convenience of jshell
Jshell is not the only function of jdk17. Sometimes you will fall in love with it. For example, if you want to write a piece of test code, you have to open idea, then build a class in the original project, write your demo in the main method, and compile and run it after writing. However, with jshell, you only need to double-click to open it, and you can program directly in the interface, and the function is very powerful
Editor heavily dependent, no prompt? Press tab, there will also be prompts
You can also type / help to see what other functions jshell has
2. Introduction of var syntax
var is not a java keyword, but a syntax sugar, which can save a lot of code workload in the process of code writing. For example, if we create a new class, we no longer need Object o = new Object(); You can use var syntax to infer the corresponding type according to the object type to the right of the equal sign.
Moreover, the new variable created by var will only be inferred and generated according to the type to the right of the equal sign. If the right of var is a number (Integer), the basic type int will be generated instead of Integer
3. Introduction of text blocks
@Test void testStr(){ String txtOld = "{\n" + "\"name\":\"website\",\n" + "\"num\":3,\n" + "\"sites\":[ \"Google\", \"FileFox\", \"Edge\" ]\n" + "}"; System.out.println(txtOld); String txtNew = """ { "name":"website", "num":3, "sites":[ "Google", "Runoob", "Taobao" ] } """; System.out.println(txtNew); }
Output results:
Use 3 quotation marks "" text "" to represent the input of text block, and there are 2 kinds of escape. Use "\" to cancel line feed and "\ s" to add spaces
@Test void testStr() { String txtOld = "{\n" + "\"name\":\"website\",\n" + "\"num\":3,\n" + "\"sites\":[ \"Google\", \"FileFox\", \"Edge\" ]\n" + "}"; System.out.println(txtOld); String txtNew = """ {\s\ "name":"website",\ "num":3,\ "sites":[ "Google"]\s\ }\ """; System.out.println(txtNew); }
Output results:
{ "name":"website","num":3,"sites":[ "Google"] }
4. switch expression
Use - > instead of the previous break
@Test void testSwitch(){ Week day = Week.FRIDAY; switch (day){ case MONDAY,TUESDAY,WEDNESDAY,THURSDAY -> System.out.println("red"); case FRIDAY -> System.out.println("yellow"); case SATURDAY,SUNDAY -> System.out.println("green"); default -> throw new IllegalStateException("what day is today?"+day); } }
The yield keyword is introduced, which is similar to the result value returned by return
@Test void testSwitch() { Week day = Week.FRIDAY; String mood = switch (day) { case MONDAY, TUESDAY, WEDNESDAY, THURSDAY -> "red"; case FRIDAY -> "yellow"; case SATURDAY, SUNDAY -> "green"; default -> { if (day == null) { throw new NullPointerException("you input null"); } else { System.out.println("what day is today?" + day); yield "blue"; } } }; System.out.println("your mood color is " + mood); }
5. Introduction of records
It is consistent with the function of lombok annotation. However, I am still used to using @ Data annotation on entity classes, because if there are too many parameters, this writing method is not elegant at all, and there is a lack of annotation on parameters
! [insert picture description here]( https://img-blog.csdnimg.cn/13cf0eabaed94c5588dde85916cb8d05.png
/** * be similar to * @Data * public class Person{ * private String name; * private Integer age; * } */ public record Person(String name ,Integer age) { }
@Test void testRecord(){ Person yuec = new Person("yuec", 123); System.out.println(yuec); }
Output results:
Person[name=yuec, age=123]
6,ZGC
Slightly.
To be improved, the comment area can be added~