JDK14, from its inception to now, its internal updates, really brought a lot of surprise to people, especially programmers, JDK14 is really attracted to this group of programmers to explore its mysterious color, from the very first five new features
JDK13:5 new features:
350: Dynamic CDS Archives
351: ZGC: Uncommit Unused Memory
353: Reimplement the Legacy Socket API
354: Switch Expressions (Preview)
355: Text Blocks (Preview)
Chinese Academy of Sciences engineers prepared for a month, and finally finished the JDK13 new feature ZGC
Today is idle, when chatting with friends, suddenly mentioned JDK14, and then went to look at the relevant documents, but also really let me find a more interesting technology -record, next, through the code to explore JDK14 record
Use the form
In fact, if you look at the code, it’s one line
Record Image(@serializedName ("media_id") String mediaId){} Image.mediaid ();Copy the code
So I’ve defined a record, an Image, and I’ve got a String mediaId in it and it looks something like this
Static class ImageClass{// Define a property private String mediaId; Public ImageClass(String mediaId) {this.mediaId = mediaId; } // define a get method public String getMediaId() {return mediaId; }}Copy the code
But now!! Now it’s just one line!! Really is the feeling of regret!!
Do wechat public number development partners should have this experience, so many nested entity classes. We’ll soon have hundreds of lines in a file. If the manager agrees, try record in JDK14
(By the way, we don’t even have JDK11 yet, do you?)
Run the configure
Since this feature is still in preview, it will require some additional configuration to work
idea
Idea need to download the 2020.1 RC version, and then select the language level 14-Preview in the project Settings, as shown in the figure below
gradle
Gradle needs to upgrade to 6.3 or later to support JDK14. Maven is not available.
You can run the following command to upgrade:
. / gradlew wrapper – gradle – version = 6.3
build.gradle
The following configuration needs to be added to the Gradle script file
tasks.withType(JavaCompile) {
options.compilerArgs += "--enable-preview"}
tasks.withType(Test) {
jvmArgs += "--enable-preview"
}
tasks.withType(JavaExec)
{ jvmArgs += '--enable-preview'}Copy the code
As a final step, change fastJson to Gson
Let’s use a main function to demonstrate this
public static void main(String[] args) { record OutRecord(String name){ } OutRecord outRecord = new OutRecord("xiaodao"); Gson Gson = new Gson(); System.out.println("gson:"+gson.toJson(outRecord)); System.out.println("fastJson:"+JSON.toJSONString(outRecord)); Init ("xiaodao"); System.out.println("gson:"+gson.toJson(holder)); System.out.println("fastJson:"+JSON.toJSONString(holder)); } static class Holder{// Declare a InnerRecord record; InnerRecord(String name){} *@param name * @return */ public static Holder init(String name){ Holder holder = new Holder(); InnerRecord innerRecord = new InnerRecord(name); holder.record = innerRecord; return holder; }}Copy the code
The following output is displayed:
As you can see, fastJson can’t be serialized with either a direct Record or a wrapper class, but Gson can be serialized with a wrapper class
Scatter flowers! Completion of this
About the record, xiaobian personal feel or very good to use, only as a value object, almost can replace the static internal class, simple code. If you want to experience the magic of code, try it yourself
Small make up to say finally, outbreak this year and has been existing Internet winter, is added to an already difficult interview, however, ladies and gentlemen, don’t be discouraged, as long as able to enrich himself at ordinary times, to prepare ahead of time, but really relatively problem is not big, want to find a job or can be, however, want to achieve the right level, Still need more efforts, for this, xiaobian prepared some such information, there is a need, can be private letter “information” access, including documents and interviews
Multithreading and high concurrency
JVM and performance optimization
23 design modes (Horse Soldier course: Tank Battle project as actual combat)
Redis Deep Adventures: Core Principles and Applied Practices
MySQL technical highlights: Architecture, advanced features, performance optimization and cluster practice
Zookeeper distributed process collaboration
Network car hailing project actual combat
Multi-level cache architecture platform for multi-level traffic
These PDF+ video materials have been sorted out for everyone, need to get friends, please help forward this article + private [information] two words
Do you believe that one document can give you a salary increase of 10K? I bought it. You got it. You can do it