Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

An example Analysis of the New characteristics of Java7

Shulou Source: shulou.com Published: 2022-05-31 11:04:06 09月18日 Update

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!

Tags: Code clips features fragments instances instance analysis analysis functions objects versions that is interfaces methods learning support keywords content syntax Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno MariaDB Docker vpn Linux