New features of jdk1.5

1. Static import (Note: This is a new feature of 1.5 . The version before 1.5 will definitely report an error):

import can import a class in a package or all classes in a package

Import can import a static method or all static methods in a class

 

import static java.lang.Math.abs;
import static java.lang.Math.*;

 

By importing the method in Math, you can use the abs method directly in the program without adding the class name.

System.out.println("max="+max(num1,num2));
System.out.println("abs="+abs(num2-num1));

2. Variable parameters

The number of parameters of a method can be indeterminate, for example:

add(num1,num2,num3);
add(num1,num2);

Features of variable parameters:

can only appear at the end of the parameter list

When calling a variadic method, the compiler implicitly creates an array for the method, so when calling, the variable array is accessed as an array in the method body.

// Definition of variable array 
    public  static  int add( int num1, int ... args)
    {
        int result=num1;
        for(int i=0;i<args.length;i++){
            result=result+args[i];
        }
        return result;
    }
    public static void main(String[] args)
    {
        int [] nums={1,1,1 };
         // Calling variable array 
        System.out.println(add(1 ,nums));
    }

3.for loop

for(type variable name: collection variable name)

{

}

illustrate:

(1) The iteration variable must be defined in ()

(2) The collection variable can be an array or a collection class that implements Iterable [hashMap class cannot be used]

(3) Modifiers can also be added in front of type, for example: final

example:

public static int add(int num1,int...args)
{
    int result=num1;
    /*for(int i=0;i<args.length;i++){
        result=result+args[i];
    }*/
    for(int i:args)
    {
        result=result+i;
    }
    return result;
}

4. Automatic boxing and unboxing of basic data types

For integers between -128 and 127, when new Integer is used, there is a pool, and these objects are taken from the pool [the idea of ​​flyweight mode is used here ]

Detailed introduction of Flyweight mode: http://www.cnblogs.com/excellencesy/p/8911003.html

Integer num1=1;
Integer num = 1 ;
System.out.println(num==num1);

Two Integers are the same object

and:

Integer num1=1111;
    Integer num = 111 ;
    System.out.println(num==num1);

are two different objects

Extend:

// The two Strings are not the same object 
String str1= new String("abc" );
String str2=new String("abc");
System.out.println(str1==str2);

Every time new is creating a new object

but:

String str1="abc";
String str2="abc";
System.out.println(str1==str2);

Two Strings are the same object

extends:

Integer num1=Integer.valueOf(3);
Integer num=Integer.valueOf(3);
Integer valueOf(int num); This static method manually turns the int object into an Integer. 
Both are the same object
Integer num1=Integer.valueOf(333);
Integer num=Integer.valueOf(333);

These two are not the same object

5. Reflection, this has been sorted out before: http://www.cnblogs.com/excellencesy/p/8868567.html

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324727388&siteId=291194637