Java Basic _jdk5 Common features

Source: Internet
Author: User


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

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.