`
terry0501
  • 浏览: 305531 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Some New features in JDK5 compare JDK1.4

 
阅读更多
 

 

1.   Enhanced 'for' loop .

 

-Java 5 added a new style of “for” loop (sometimes called "for-in" loops) 

 5.0

1.4

for (type variable : array){

  // body

}

 

for (int i = 0; i < array.length; i++){

   type variable = array[i];

   //body

}

for (type variable:arrayList){

   //body

}

for (int i=0;i<arrayList.size();i++){

   type variable =(type)arrayList.get(i);

   //body

}

– This code loops through each entry in an array or collection object returning one value at a time 

for processing -- an Iterator is used without an Iterator being defined!

2. Generics

We use ‘ArrayList’ ,as example .

5.0

1.4

ArrayList<Employee> arrayList =

      new ArrayList<Employee>()

ArrayList arrayList =new ArrayList();

Employee e=arrayList.get(i)

Employee e=(Employee) arrayList.get(i)

      Now with this new feature you don't need so much force convert.

3. Enum
    Another new Java 5 feature that looks familiar to C programmers is the "Enum" data type. Enum allows assignment of a specific set of values

 to associated .You can see many in JDK source 1.5.
       public class UsingEnum {
          public enum Weekdays {
             Thursday, Friday,  Monday, Tuesday, Wednesday,
             Saturday, Sunday};
          }
        }

4. AutoBoxing

Java 5 (Java 1.5) and later automatically “box”and ”unbox” values.

 – The advent of automatic Boxing and automatic Unboxing greatly simplifies code when using 
 Collection and other types of objects 

– Boxing and Unboxing is also important since Primitive data and Reference Type data are stored in different places; primitives representing local variables are stored on the stack while objects are stored in heap memory.

    Example:

5.0

1.4

Integer wrapper = n;

Integer wrapper = new Integer(n);

 

5.0

1.4

int n = wrapper;

int n = wrapper.intValue();

 

5. Variable Argument Lists

                                                   

New variable argument lists (VarArgs) allow specification of a method that can accept a final parameter of the same type with the number of values to be determined at runtime. Below are some rules related to this.

- Only one variable argument list is allowed per method

– Variable list must be the last argument defined for the method

– The ellipsis "…" is used to indicate that an argument might appear a variable number of times

Example:

 

5.0

1.4

method(other params, p1, p2, p3)

method(other params, new Type[] { p1, p2, p3 })


public Auto (String year, String make, String model, String... options) { … )
............
Auto myTruck = new Auto("1997","Ford","Four-wheel drive","power windows", "air-conditioning");

6. Changeable return value

 

Before JDK5.0you can’t change the return value.Now with this new feature You can do it here is an example.

5.0

1.4

public Employee clone() { ... }

...

Employee cloned = e.clone();

 

public Object clone() { ... }

...

Employee cloned = (Employee) e.clone();

 

 

7. Static Imports

                                         

Java 5 (Java 1.5) allows import of static items when methods or variables are used repeatedl but JDK 1.4 doesn’t support

5.0

1.4

import static java.lang.Math;

import static java.lang.System;

...

out.println(sqrt(PI));

System.out.println(Math.sqrt(Math.PI));

 

 

8.Metadata Annotations

 

Java 5 introduced a method for adding metadata to package and type declarations, methods, 
constructors, parameters, fields, and variables. Some of them are
- @Override
- @Deprecated
- @SupressWarnings

9. Scanner Input

                                              

JDK 5.0 does’t have Scanner classyou need use JOptionPane.showInputDialog class

5.0

1.4

Scanner in = new Scanner(System.in);

System.out.print(prompt);
int n = in.nextInt();
double x = in.nextDouble();
String s = in.nextLine();

String input = JOptionPane.showInputDialog(prompt);
int n = Integer.parseInt(input);
double x = Double.parseDouble(input);
s = input;

10. StringBuilder.class

The new class, java.lang.StringBuilder provides a faster alternative to StringBuffer
– In most ways StringBuilder works exactly the same as StringBuffer
– StringBuilder is faster than StringBuffer because it is not ThreadSafe (multiple threads should not access StringBuilder objects without Synchronizing)
– Use StringBuilder when speed is important in a single-thread environment and use StringBuffer if multiple threads might require access.

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics