Jdk5 new Features
1.JDK new features-enhanced for
(1). Format:
For (element data type variable: Array or collection collection) {
Use the variable, which is the element
}
(2). Benefits
Simplifies the traversal of arrays and collections.
(3). Disadvantages
The target of the enhanced for is not NULL
(4). Code implementation:
public class Test {
public static void Main (string[] args) {
List<string> lt=new arraylist<string> ();
Lt.add ("Hello");
Lt.add ("World");
Lt.add ("Java");
for (String s:lt) {
System.out.println (s);
}
}
}
2.jdk5 new features: Static import
(1). Format: Import static package name ... Class name. method name;
The level at which the method can be imported directly.
(2). Considerations for static Import:
A: The method must be static
B: If you have more than one static method with the same name, who is it easy to know? This is a time to use and must be prefixed. This shows that the meaning is not big, so generally not, but need to understand
C: Direct import of the jar package below the required method, so you can omit the need to write the class name.
(3). Code implementation
public class Test {
public static void Main (string[] args) {
System.out.println (Math.Abs (-0.123));
System.out.println (Math.pow (2, 3));
System.out.println (Math.max (20,30));
}
}
The subsequent static import is much easier to upgrade with the JDK version.
3.jdk5 new features: Variable parameters
(1). When defining a method, you don't know how many parameters to define
(2). Format:
Modifier returns a value type method name (data type ... Variable name) {
}
(3). Precautions for use:
The variable is actually an array, and if a method has a variable parameter, and there are multiple parameters, then the variable parameter is definitely the last one (that is, the match is not complete will be variable
Parameters are fully accepted)
(4) Code implementation
public class Test {
public static void Main (string[] args) {
int resutl=sum (1,2,3,4,5,6,7,8,9,10);
System.out.println (RESUTL);
}
/**
* Variable parameter use Test
* @param int ....
* @author Administrator
* @return Int:result
*
*/
public static int sum (int ... a) {
int resultvalue=0;
for (int i:a) {
Resultvalue+=i;
}
return resultvalue;
}
}
Output results: 55
Java Basic _jdk5 Common features