Java5 static import of new features, variable parameters, enhanced for loop, automatic unboxing

Source: Internet
Author: User

JDK1.5 has been released for a long time and has been taken out because it adds a number of important features that help us simplify development, write code that is more concise and clear and secure, with the following features:

? Static Import

? Variable parameters

? Enhanced for Loop

? Automatic unboxing

? generic type

? Enumeration

Because generics, enumeration content is more, but also the most important, after the single out of the speaking. Here we introduce the first four simple and practical small features.


1. Static Import

The so-called "static import" is simply adding the keyword static to the normal import statement, for example:

? Non-static imports: import Java.lang.Math;

? Static import: Import static java.lang.math.*; or import static Java.lang.Math.max;

The functionality of static import is simple:

? First, a static import can import a method (static) Under a class, or all methods, and not static imports can only navigate to a class.

? Secondly, the function of static two words is on the call of static method . If you are non-static, use the class name when calling a static method of a class. Method Name ", if it is static import, then call the class static method can omit the class name, directly with" method name ", the example is as follows:

1) Non-static import:

  

2) Static import:

  


2. variable Parameters

Variable parameters are said to be a method, the number of parameters received is dynamically variable. For example, we want to encapsulate a method so that it can calculate the addition of any number of numbers:

public class Variableparameter {//the method of calculating the addition of any number of integers public static int add (int ... args) {int sum = 0;for (int i = 0; i < args.length; i++) {sum = Sum+args[i];} return sum;} public static void Main (string[] args) {//Test 1: Calculates the addition System.out.println (add) of 2 integers;//Output 3// Test 2: Calculate the addition of 3 integers System.out.println (Add (10, 20,30));//Output 60}}

Usage Rules:

1) If there are multiple parameters, the mutable parameter can only appear at the end of the parameter list: for example:publicstatic int add (int x, int y,int ... Args) {...}.

2) ... Is between the variable type and the variable name (there are no spaces before and after it is possible).

3) When you call a method of a mutable parameter, the compiler creates an array for the mutable parameter implicitly, accessing the mutable parameter as an array in the method body.


3. Enhanced for Loop

Very simple, look directly at the example:

public static int Add (int x,int ... args) {int sum = x;//normal for loop/*for (int i = 0; i < args.length; i++) {sum = Sum+args[i];} *///Enhanced for loop for (int arg:args) {sum=sum+arg;} return sum;}

Usage Rules:

1) syntax, for (type variable name: Collection variable name) {...}, for example: for (int Arg:args) {...}

2) The iteration variable must be defined in (), for example: for (int arg : args) {...}

3) The Set variable can be an array or a collection class that implements the Iterable interface


4. Automatic unboxing

Unboxing is a reference type and value type of mutual conversion, JAVA5 used to be manual disassembly box, the concept is not much to say, directly see the example:

public class Autobox {public static void main (string[] args) {//automatically wraps the base data type (value type) as an integer (reference type) object (boxed) integer a = 1;integer b = 1; System.out.println (A = = B);//Output  Trueinteger a1 = 130;integer B1 = 130; SYSTEM.OUT.PRINTLN (a1 = = B1);//output false//from 128 to 127 because of the more frequent use, so the same number will be wrapped in the same object (such as the above A, b), where the "enjoy meta mode"//Manual Boxing Integer A2 = integer.valueof (1); Integer b2 = integer.valueof (1); SYSTEM.OUT.PRINTLN (A2 = = b2);//Output True}}

As mentioned in the code comment, the number between 128 and 127 is very common, so in order to save memory, automatic boxing is applied to the "Enjoy meta mode". The code, integer a = 1 and integer b = 1, appears to be a, b two objects, in fact they point to the same piece of memory space. While the integer a1 = 130 and Integer b1= 130, the A1 and B1 point to different memory spaces, so they are two different objects.

Enjoy meta-thinking: There are a lot of small objects, they have a lot of properties of the same, they will be made into an object, the different attributes into the parameters of the method, when necessary to pass in. The same property is called the internal state of the object, and different properties are called external states. It is suitable for a large number of objects that are simply duplicated and use an unacceptable amount of memory


5. Summary

The above few small features, seemingly very simple, but if skilled use will greatly improve our coding efficiency, improve code quality and performance. Enumerations and generics are the most important two features introduced by Java5, in which the generic idea is described in the previous article. Enumerations are seemingly simple, and the benefits are many, and it is a feature that has to be made up after the beginning of Java, and will be described later in this article.

Java5 static import of new features, variable parameters, enhanced for loop, automatic unboxing

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.