In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "Java7 new feature example analysis", the explanation content in the article is simple and clear, easy to learn and understand, please follow the small series of ideas slowly in-depth, together to study and learn "Java7 new feature example analysis" bar!
Java 7's new feature is to add support for String in the switch block, although it is only adding String, but it is much better than previous versions that only support Integer. This feature is supported in C#1.0, and not only String, all objects can be used in the switch block.(Correction: C#2.0 switch can only use bool,char,integer,enum,string and corresponding nullable types, A switch expression or case label must be a bool, char, string, integral, enum, or corresponding nullable type).
;The try-with-resource Statement
This new Java 7 feature is familiar to C#2.0 programmers. When coding resources that need to be released in time, the usual practice is to call close () in the finally block to release them. C#provides a simple method to achieve the same function. The code is as follows:
Here is the code snippet:
;using(SqlConnection conn = new SqlConnection("ConnectionStringHere)){
;// Do something
;}
The above code is equivalent to:
Here is the code snippet:
;SqlConnection conn = new SqlConnection("ConnectionStringHere);
;try{
;conn.open();
;// Do somethind
;} finally{
;conn.close();
;}
Java 7 implements a similar functionality, but instead of using, it uses try, as follows:
Here is the code snippet:
;try (BufferedReader br = new BufferedReader(new FileReader(path))) {
;return br.readLine();
;}
For C#, using must satisfy a condition that the object declared in using implements the interface System.IDisposable, so that the code in the finally block can automatically call Dispose () under the interface.
Method to achieve the purpose of releasing resources. The same requirement applies to Java 7, where objects must implement the java.lang.AutoCloseable or java.io.Closeable interfaces.
;The For-Each Loop
Java 7 finally implements for-each loop functionality, although it is a syntax enhancement in Java5, but since Java6 has no syntax update, I list this enhancement as a new feature in Java 7. However, I don't understand why I still use for as a keyword, instead of directly introducing foreach as in C#. Is this easier to understand? Here is the Java version of for-each code:
Here is the code snippet:
;void cancelAll(Collection c) {
;for (TimerTask t : c)
;t.cancel();
;}
For the C#version, the code is:
Here is the code snippet:
;void CancelAll(Collection c) {
;foreach (TimerTask t in c)
;t.Cancel();
;}
As you can see, the implementation of the two is not much different, the same simplicity and clarity, but the scope is different, Java 7 seems to be implemented only in collections (including traditional arrays and generic collections), while C#can be used in any object that implements System.IEnumerable or its generic version System.IEnumerable.
Thank you for reading, the above is "Java7 new features example analysis" of the content, after the study of this article, I believe we have a deeper understanding of Java7 new features example analysis of this issue, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!
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
© 2024 shulou.com SLNews company. All rights reserved.