A brief summary of the new features of Java1.5 language

Source: Internet
Author: User
Tags add new features printf string version
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 ();



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.