In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article shows you how to automatically fill in the public fields of SQL statements in Java, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Such as the following:
1. Use comparison
Next, we will use the way of comparison to experience the role of var.
Scenario 1: define a string
Old way of writing:
String str = "Hello, Java."
The new way of writing:
Var s = "Hello, Java."
PS: the old way of writing here refers to the version before JDK 10, while the new method refers to the later version of JDK 10 (including JDK 10).
Scenario 2: numerical addition
Old way of writing:
Int num1 = 111; double num2 = 555.666d; double num3 = num1 + num2; System.out.println (num3)
PS: the upward transformation of data types occurs when different types are added (int+ double), so num3 is upgraded to double types.
The new way of writing:
Var N1 = 111L; var N2 = 555.666; var n3 = N1 + N2; System.out.println (n3)
Scene 3: assemble
Old way of writing:
List list = new ArrayList (); list.add ("Hello"); list.add ("Java")
The new way of writing:
Var list = new ArrayList (); list.add ("Hello"); list.add ("Java")
Scenario 4: loop
Old way of writing:
For (Object item: list) {System.out.println ("item:" + item);} for (int I = 0; I
< 10; i++) { // do something... } 新写法: for (var item : list) { System.out.println("item:" + item); } for (var i = 0; i < 10; i++) { // do something... } 场景五:配合 Lambda 使用 旧写法: List flist = list.stream().filter(v ->V.equals ("Java"). Collectors.toList (); System.out.println (flist)
The new way of writing:
Var flist = list.stream () .filter (v-> v.equals ("Java")) .Collectors.toList (); System.out.println (flist)
2. Advantage analysis.
From the above example, we can see that var has two obvious advantages: improved code readability and naming alignment.
① improves readability
Before we use var, this happens if the name of the type is very long:
InternationalCustomerOrderProcessor orderProcessor = createInternationalOrderProcessor (customer, order)
When you limit each line to no more than 150 characters, the variable name is pushed to the next line, making the whole code less readable. But when we use var, the code looks like this:
Var orderProcessor = createInternationalOrderProcessor (customer, order)
As you can see from the above code, the longer the type, the greater the value of var.
② naming alignment
When you are not using var, the code looks like this when you encounter the following situations:
/ / explicit type No no = new No (); AmountIncrease more = new BigDecimalAmountIncrease (); HorizontalConnection jumping = new HorizontalLinePositionConnection (); Variable variable = new Constant (6); List names = List.of ("Java", "Chinese community")
After using var, the code looks like this:
Var no = new No (); var more = new BigDecimalAmountIncrease (); var jumping = new HorizontalLinePositionConnection (); var variable = new Constant (6); var names = List.of ("Java", "Chinese Community")
From the above code, you can see that after using var, the naming is aligned and the whole code becomes more elegant.
3. Rules of use & counterexamples
The implementation of var is from JEP 286 (improvement proposal 286), details: http://openjdk.java.net/jeps/286
As can be seen from the title "Local variable Type inference" of JEP 286, var can only be used for local variable declarations, which means that var must meet the following conditions:
It can only be used on local variables.
Must be initialized when declaring
Cannot be used as method parameters and global variables (class variables).
PS: because the implementation of var must infer the type from the code on the right, it cannot be assigned null or not initialized.
Counterexample 1: uninitialized and assigned null
Counterexample 2: midway type change
Counterexample 3: global variables
Counterexample 4: as a return value
4. Principle analysis.
After the previous use, we have a preliminary understanding of var, but what is the implementation principle of var?
To figure out how it works, we compiled the following code (using the command javac MainTest.java):
Then we use the decompiler to open the compiled class and find that var has been replaced with a certain data type, as shown in the following figure:
From this we can draw a conclusion: the implementation of the var keyword is closely related to its name, var is only local type inference, it will only be valid in the Java coding period and compilation time, when the class is compiled into a class file, the var will become a certain data type (inferred). So we can think of var as the syntax sugar of Java, which allows us to implement business code quickly and elegantly, but var does not exist at the bytecode level.
The above is the public field of how to automatically populate SQL statements in Java. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
☞ sets up a single host and three members MGR
© 2024 shulou.com SLNews company. All rights reserved.