1. Automatic boxing and unpacking correspond to C #
Example 1.1
Integer i = 10;
int j = i;
2. More optimized for loop corresponding to C #---foreach Loop
Example 2.1
String[] names = {"BadBoy", "Goodboy", "Happygirl", "Sadgirl"};
for (String option:names) {
SYSTEM.OUT.PRINTLN (option);
}
Example 2.2 plus generics corresponding C + + template
Import java.util.*;
arraylist<string> animals = new arraylist<string> ();
Animals.add ("Dog");
Animals.add ("Cat");
Animals.add ("Chick");
Animals.add ("Cow");
for (String option:animals) {
SYSTEM.OUT.PRINTLN (option);
}
3. Variable parameter methods and printf
Example 3.1
Defined:
public int sum (int ... n) {//pass n is an int type array
int tempsum;
for (int option:n) {
Tempsum+=option;
}
/*
for (int i = 0; i < n.length; i++) {
Tempsum+=n[i];
}
*/
return tempsum;
}
Call 1:sum (1);
Call 2:sum (1,2);
Call 3:sum (1,2,3,4);
Example 3.2 printf method, corresponding to the C language of printf
int x = 10;
int y = 20;
int sum = x + y;
System.out.printf ("%d +%d =%d", x,y,sum);
4. Enumeration
Example 4.1
public enum MyColors {
Red
Black
Blue
Green
Yellow
}
MyColors color = mycolors.red;
For (MyColors option:color.values ()) {
SYSTEM.OUT.PRINTLN (option);
}
/** cannot write case mycolors.red in a switch statement:
* So the compiler won't let you through * *
switch (color) {
Case RED:
SYSTEM.OUT.PRINTLN ("Best color is" +red);
Break
Case Black:
System.out.println ("NO" + black);
Break
Default
System.out.println ("What");
Break
}
5. Static reference
Example 5.1
The previous version of 1.5 is:
Import Java.lang.Math; At the beginning of the program
...
Double x = Math.random ();
This can be written in version 1.5
Import static java.lang.Math.random; At the beginning of the program
...
Double x = random ();