Home > Java > javaTutorial > Example analysis of new features of Java7

Example analysis of new features of Java7

PHPz
Release: 2023-05-07 21:04:09
forward
1155 people have browsed it

Java7's new feature is to add support for String in the switch code block. Although it only adds String, it is much better than the previous version which only supported Integer. This feature is available in C# 1.0 is supported in the switch block, 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 the corresponding nullable value 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 feature of Java7 is very familiar to C# 2.0 programmers. When coding some resources that need to be released in time, the usual approach is to finally block Call close() to release, and C# provides a simple method to achieve the same function, the code is as follows:
;The following is a code fragment:
;using(SqlConnection conn = new SqlConnection(" ConnectionStringHere)){
;// Do something
;}
;The above code is equivalent to:
;The following is the code snippet:
;SqlConnection conn = new SqlConnection("ConnectionStringHere) ;
;try{
;conn.open();
;// Do somethind
;} finally{
;conn.close();
;}
;And Java7 implements a similar function, but instead of using using, it uses try. The code is as follows:
;The following is a code fragment:
;try (BufferedReader br = new BufferedReader (new FileReader (path))) ) {
;return br.readLine();
;}
For C#, using using must meet a condition, that is, the object declared in using implements the interface System.IDisposable. In this way, the finally block The code in can automatically call the Dispose()
method under this interface to achieve the purpose of releasing resources. The same requirement exists for Java7, that is, the object must implement the interface java.lang.AutoCloseable or java.io.Closeable.
;The For-Each Loop
Java7 finally implements the for-each loop function. Although it is a syntax enhancement in Java5, since there is no syntax update in Java6, I will list this enhancement as a new feature of Java7. But I don’t understand why we still use for as the keyword instead of directly introducing the foreach keyword like C#. Is it easier to understand this way? Here is the Java version of for-each code:
;The following is the code snippet:
;void cancelAll(Collection c) {
;for (TimerTask t: c)
;t.cancel() ;
;}
;For the C# version, the code is as follows:
;The following is the code snippet:
;void CancelAll (Collection c) {
;foreach (TimerTask t in c)
;t.Cancel(); (including traditional arrays and generic collections), and C# can be used in any object that implements System.IEnumerable or its generic version System.IEnumerable.

The above is the detailed content of Example analysis of new features of Java7. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template