Java new feature 1 (j2se entry 21)

Source: Internet
Author: User

New Features of java5.0

Automatic packing and automatic unpacking
Automatic packing and automatic unpacking enable automatic conversion when simple types and encapsulation types are converted to each other.

The automatic binning solution is only available when necessary. If there are other options, use other
Byte B-128 ~ 127
One more null value for byte B

Differences between simple and encapsulation types
The encapsulation class can be null to avoid ambiguity when the number is 0.
Integer I = NULL;
Int II = I; will throw the nullexception exception.
Equivalent to int II = I. intvalue ();
Integer I = 1; equivalent to integer I = new INTEGER (1 );





Automatic conversion between basic data types and encapsulation classes

<Before 5.0>
Integer I = new INTEGER (4 );
Int II = I. intvalue ();

<After 5.0>
Integer I = 4;
Long L = 4.3;

Static Introduction
Use of static membersImport staticIntroduce static members, that is, you can use static introduction to be a static member of a class in the import package. You do not need to write the class name when using it.
Something simple
Example:
<No static import>

Math. SQRT (math. Pow (x, 2) + math. Pow (Y, 2 ));

<With static import>

Import static java. Lang. Math .*;
SQRT (POW (x, 2) + POW (Y, 2 ));

WhereImport static java. Lang. Math .*Is the static import syntax, which means to import all static methods and attributes in the math class. In this way, you do not need to write the class name when using these methods and attributes.

Note that the default package cannot be statically imported. If the imported class contains repeated methods and attributes, you need to write the class name; otherwise, the class name cannot be passed during compilation.

Enhanced for loop (for each)
The for-each loop facilitates the unification of arrays and sets, and solves the inconsistency between traversing arrays and traversing sets.

Import java. util .*;
Import java. util. collection;

Public class foreach {
Private collection <string> C = NULL;
Private string [] Belle = new string [4];
Public foreach (){
Belle [0] = "Xi Shi ";
Belle [1] = "Wang Zhaojun ";
Belle [2] = "mink Chan ";
Belle [3] = "Yang Guifei ";
C = arrays. aslist (Belle );
}

Public void testcollection (){
For (string B: C ){
System. Out. println ("past weathering:" + B );
}
}
Public void testarray (){
For (string B: Belle ){
System. Out. println ("historical records:" + B );
}
}
Public static void main (string [] ARGs ){
Foreach each = new foreach ();
Each. testcollection ();
Each. testarray ();
}
}


For the collection type and array type, we can use the foreach syntax to access it. In the above example, we used to access arrays in sequence, which is quite troublesome:

For (INT I = 0; I <Belle. length; I ++ ){
String B = Belle [I];
System. Out. println ("past weathering:" + B );
}


Now you only need the following simple statement:

For (string B: Belle ){
System. Out. println ("historical records:" + B );
}

The access to the set is more effective. Previous Code for accessing the collection:

For (iterator it = C. iterator (); it. hasnext ();){
String name = (string) it. Next ();
System. Out. println ("past weathering:" + name );
}

Now we only need the following statement:

For (string B: C ){
System. Out. println ("past weathering:" + B );
}

Disadvantages of for each:
In the previous code, we can use iterator to perform the remove operation.

for (Iterator it = c.iterator(); it.hasNext();){
it.remove();
}

However, in the current for-each version, we cannot delete the objects contained in the set. You cannot replace objects either.
At the same time, you cannot parallel for-each multiple sets. Therefore, when writing code, we have to use it based on the situation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.