Starting this week, I’m launching a new series on Java.
In a 2021 survey in IDEA, Java 8 was still the dominant version of Java among programmers. Java 17, the new long-supported version of Java 11, has not been as popular as Java 8.
I don’t necessarily believe in a new version of Java, but it’s important to realize that Java 8 was released in 2014, eight years ago. Over the past eight years, languages like Kotlin, Swift, and TypeScript have been constantly updating and optimizing their language features.
This makes Java 8 less and less elegant in terms of code. Fortunately, Java has not stopped its march. In the many versions since Java 8, Java has developed new syntactically features that make code more concise, building on the best features of other languages.
Let’s take a look at some of the new language features that deserve our attention.
In this first post, LET me talk about the local variable var.
Variables and Constants
In the case of declaring variables, the familiar Java variable declaration is:
/ / variable
EntityRepository entityRepository = new EntityRepositoryJPA();
/ / constant
final String httpMethod = "post"
Copy the code
Java variables are declared by class + name, or by final keyword if constant.
We can compare how variables are declared in other languages
In Kotlin, variables are declared with var and constants are declared with val
/ / variable
var entityRepository = EntityRepositoryJPA()
/ / constant
val httpMehod = "post"
Copy the code
TypeScript declares variables in lets and constants in const
/ / variable
let entityRepository = new EntityRepositoryJPA()
/ / constant
const httpMethod = "post"
Copy the code
In Swift, var defines variables and let defines constants
/ / variable
var entityRepository = EntityRepositoryJPA(a)/ / constant
let httpMethod = "post"
Copy the code
As you can see from the above comparison, the new languages all prefer the keyword + name pattern over the Java type + name definition.
Automatic type determination
In fact, modern programming languages like to use type autodetermination to the maximum, that is, the keyword + name pattern.
The basic principle of type presumption is that as long as you can guess from the context, you do not need to explicitly state its type
Because one obvious point is that this code is actually cleaner.
If we were to rewrite the above Java variable and constant definitions using keyword + name, our code would look like this:
// Overwrite with (keyword + name) pattern
/ / variable
var entityRepository = new EntityRepositoryJPA();
/ / constant
var httpMethod = "post"
Copy the code
Based on the logic of type automation, it’s pretty obvious to the compiler and us programmers that the entityRepository type is an instance of the EntityRepositoryJPA class, and httpMethod is a String.
Of course, the above example may not feel like it’s necessary because it’s not as simple as it should be, but in some complex scenarios it can be a lot simpler
// Use the old patternCollector<String, ? , Map<String, Long>> byOccurrence = groupingBy(Function.identity(), counting());// Use var to override
var byOccurrence = groupingBy(Function.identity(), counting());
Copy the code
Syntax parsing
So Java 10 introduced the local variable var keyword for one of the most obvious reasons: to simplify code
It is hard to argue that this feature has not been borrowed from other mainstream modern languages, and I think it certainly has been.
However, due to Java’s long history, this feature is only a half-baked implementation compared to other languages, and it has many limitations
- The var keyword can only be used in methods, not method parameters, class parameters, etc
- Var is the meaning of a variable, there is no keyword to simplify constants
One of the biggest limitations is that you can only use the var keyword in local variables in a method
@Test
void testEntityExists(a){
var exists = repository.exists(User.class,-1L);
Assertions.assertFalse(exists);
var created = repository.save(randomUser());
exists = repository.exists(User.class,created.getId());
Assertions.assertTrue(exists);
}
Copy the code
As the code above shows, you can only use var inside a method, not anywhere else, and it represents variables; there is no corresponding keyword to simplify constants.
Disadvantages and Impacts
I’m not going to go into that, but the biggest and basically the biggest advantage is that it makes the code simpler.
Let’s talk about the cons.
Speaking from my own experience, I see the disadvantages of this feature for long-time Java language users as follows:
- Java programmers are not used to this style
If you are a front-end, mobile programmer, whose primary programming language is basically a keyword + name pattern, you will be familiar with this style.
For me, for example, and I really liked it when I learned about it, I switched to this mode almost instantly, because I’m used to the keyword + name style in all the other languages.
Programmers who have been working in Java all their lives, however, are so familiar with the class name + name style that there is no particular need to use this half-baked feature.
We all love our familiar styles, don’t we?
- Local optimization rather than global transformation
This change in Java is not global, you can’t use this style in class variables, method parameters.
As a result, the impact of this transition is relatively small, which may further exacerbate the ignorance of this feature.
- This affects the readability of the code
Well, we all know that brevity and readability can sometimes go in different directions; The briefer it is, the harder it is sometimes to read, and the more verbose it may be, the easier it is to read.
This style, for those who are used to it, has no impact on readability, but for Java programmers it still feels a little less readable.
In this regard, the IDEA tool provides a very interesting tool aid to enhance readability. See the figure below
See, IDEA automatically displays the type of var.
Why is IDEA doing this? It must be that Java programmers are less familiar with this style and use it to help and alert programmers.
But from the perspective of someone who speaks a lot of other languages, like me, there’s not much need for that. In fact, when Kotlin is used in IDEA, there is no such hint at all.
Refer to the figure below:
As you can see, this tip is specially prepared for Java, very thoughtful.
Progress to be appreciated
Once I learned that Java had local variables, I did make the switch very quickly, at almost no cost, influenced by my past use of other languages. And from my coding feel, it does make the code cleaner, that’s for sure.
But for programmers who use Java from start to finish, I think there is some cost to the transition.
But it’s worth it for the sake of simplicity.
Of course, it’s up to you to do whatever you want.
At this point, HOWEVER, I am quite impressed with the Java language, which has not stopped learning from other modern languages to improve itself.
With so many versions of the language updated from Java 8 to the latest version, Java 17, this is certainly not the only improvement.
Just because we love Java, it’s worth following the language we love and not sticking with the version of eight years ago, right?
In the next article, I’ll continue talking about interesting new features coming out of Java 8.
Taoofcoding. tech – Spread the word ofcoding with our little power
Visit myDDD – Full stack Domain Driver website: myddd.org