Original text: http://www.theserverside.com/blogs/showblog.tss? Id = JDK5Practice (please indicate the source, the original author and the translator, thank you)
JDK 5 Practice - by Cedric Beust November 5, 2004
I have written JDK 5.0 code has been more than 6 months, I think I can spend some time to organize my experience and make some summary of newly introduced language functions.
Enhanced for loop
This new feature is a no controversial champion. I don't even know how to describe how pleasant things in all places (almost all places) use new For loops. I have learned from psychologically to those who I have to use old for loops. If I need to index or hope that iterators can be seen outside of the cycle.
The new code is more readable and unlike the past, it is full of noise (such as the index you don't need, or the additional incremental increment). By the way, this is the unexpected advantage of the new cycle. Suppose you have the following code:
String [] name = ...; for (string name: names) {// ...}
Then you decide to change the type of Names to Collection. How do you modify your code?
List
Yes, that is, this is only necessary to modify one line. There is no better than this.
Annotations
Obviously, I prefer to decline because they are TestNG (Translator Note: TestNG is the core of the original author's authors, Java annotation), but I firmly believe that the annotation will change the way we use Java to write software. It has already had a long period of time relies on reflex in our procedure to introduce metadata, and the annotation mechanism can finally provide an excellent way to solve this problem.
At the same time, since some predefined annotations are not required, I have no more insights.
In my opinion, most Java code we read and prepared in the next few years will contain an annotation, which is inevitable.
Static reference
In addition to my annotation, I rarely use them in my annotation, and Target. I am still not very confident that I originally increased this feature (not encouraged to retrofit its constant by implementing an interface without limiting and verifying the "reverse mode") of their "reverse mode") sufficiently correct to introduce a new language function. But time will give us an answer.
I want to say in a sense, using IDE in my daily programming has made the reference to get completely, so I probably not really feel a strong feeling about this new feature.
Variable length parameters
I haven't felt this feature so far. It may be more convenient when it is time, but I really don't believe it needs to change the language.
enumerate
Although I will definitely give theoretical first hospitality, I haven't really converted my code into the use of them, and I have not used their ideas for the time being. I believe that when I use them, the result will satisfy me and will make my code more robust.
Generic
I put the best to stay in the end ... but because this entry will be more long, I will leave generic discussion to tomorrow.
Automatic packing
At present, I have not used it too much to use the automatic packing, maybe because I have a feeling of losing my code performance control. However, I think this doesn't have any actual truth, and the automatic packing sometimes uses it easy to make your code more read. I think I will encourage developers to make tags in the code in the code, I am quite confident that Ide will follow this function.
Generic
Where is it from?
First of all, no one will oppose the genericity is a reliable concept that enhances the robustness of code. As for why they usually controversial as they do not consider the language factor is because they are implemented. With my experience in the C committee for many years, I can guarantee how difficult it is to do it. Simply put, I have described Java generics: My code became more robust, but it is more difficult to understand.
So what is the problem?
Unnecessary repetition.
First, I don't like the unnecessary repetitions that need to be brought by general transformation from the beginning. For example, with its writing:
Map Accounts = new hashmap (); // no generics ... Account a = (account) Accounts.get ("CEDRIC");
Why can't I write simple?
Map m = new hashMap (); // no generics ... Account a = m.get ("CEDRIC");
Then introduce the compiler into an implicit transformation? Because it is clear that I want to take out an Account class from Map. Obviously, generics does not completely solve this problem, but it is also quite good to alleviate some. However, they also make things worse in other places:
Map
Ugh.
This code is not only more difficult to read, but also violates the Dry principle ("Don't repeat yourself"). What should I do if I need to change this MAP value from list
I acknowledge that when you create a new object, there is nothing a good way to avoid this grammar, but I want to show that I think if TypedEf is also introduced simultaneously, generics may do better.
At least I think so.
When I think of this problem, I realize that Typedef is not the correct way to solve this problem, because of the simple speaking, they can only use a separate class to define your complex generic type, except that they can't Do more things.
Class Accountmap Extends Hashmap
Unless you need to inherit an implementation class (HashMap instead of MAP, this program may be better than introducing Typedef, Typedef also has its own problems.
So far, I haven't met this trouble, but my suggestion is: When you write more than three times (initialization, then more than once in the code), use this Way.
In addition to this small trouble, I am very satisfied with the generic general, especially when I read the TestND category so good Javadocs.
Conclusion
I am very happy with the new functions of JDK 5.0, and I am honored to have an opportunity to influence it by participating in JSR 175 and JSR 201. Just like all of the radical evolution, not all functions will be welcomed by everyone, but as long as most developers feel useful, and ensure that JDK 5.0 is Java, Java, a more robust code. A solid step.
About the author CEDRIC beust Cedric@beust.comblog: http://www.beust.com/weblog/cedric beust is a senior software engineer of the WebLogic Server team, and often published on his WebLog Otaku to J2EE, Java, AOP, And the insights developed by software.