Difference
between java 1.4 and java 1.5 and Java 1.6 New features?
1. Generic Type
We can strict the type of values to be stored in the collection.
Ex:
ArrayList<Double> data = new ArrayList<Double>();
2. Auto boxing and Unboxing
Autoboxing is a new feature offered in the Tiger (1.5) release of
JDK. In short Autoboxing is the automatic conversion between
primitive types and reference types.
One of the petty nuisances has been removed from working with the
collections objects as well. Most of the time, raw numeric data will be stored
in primitive, non-object data types like int and double. However, only
their object, class-based equivalents can be stored in a collection
Ex:
Before
|
ArrayList<Integer>
list = new ArrayList<Integer>();
list.add(0,
new Integer(42));
int total =
(list.get(0)).intValue();
|
After
|
ArrayList<Integer>
list = new ArrayList<Integer>();
list.add(0,
42);
int total =
list.get(0);
|
3. Advanced For loop
When storing data in an array or any of the collection data structures,
the programmer can shorten the traditional for loop from the familiar.
for (int x = 0; x < data.size(); x++) {
// do something with each double in "data"
}
To the more compact and easily read:
for (Double d : data) {
// do something with each double in "data"
}
Example:
Traditional For Loop
|
1
2
3
|
for (int i=0; i < arrayOfElements.length; i++) {
System.out.println("Element: " +
arrayOfElements[i]);
}
|
Enhanced For Loop
|
1
2
3
|
for (String element : arrayOfElements) {
System.out.println("Element: " + element);
}
|
When to use enhanced For Loop….
Enhanced for loops may be simple but inflexible. You don’t have access
to Loop Index. Prefer Enhanced For Loop only
· If you have to
iterate through all the elements.
· If you don’t need
to know the loop index of the current element
· If you need to
iterate through the Collection/Array in first-to-last order.(In other
words, if the order doesn’t matter to you)
4. Enumerated type
Enum in Java is a keyword, a feature which is used to represent
fixed number of well known values in Java, For example Number of days in
Week, Number of planets in Solar system etc. Enumeration (Enum) in Java was
introduced in JDK 1.5.
One of the common use of Enum which emerges is Using Enum to
write Singleton in Java, which is by far easiest way to implement
Singleton and handles several issues related to thread-safety, Serialization automatically
Java 1.5 added the "enum" type to the language. Traditionally,
if a programmer wanted to keep track of all the possible values of an option,
he or she had to map words to integers using the "final static int"
data type, like so:
public class Stoplight {
int color = 0;
final static int RED = 0;
final static int YELLOW = 1;
final static int GREEN = 2;
}
This was tedious and bug-prone. For example, what happens if color
becomes "4?" Enum makes the solution far more elegant:
Stoplight Color = Stoplight.RED;
public enum Stoplight {
RED, YELLOW, GREEN
};
Unlike the old "final static" way, the compiler now knows what
valid options are for "Stoplight" and can enforce them for us. As an
added bonus, if the value of "Color" is printed to the console, it
will print as the English word, and not as a number.
Syntax:
type … variable Name.
Some points which should be taken care when use varargs:
1.
Ellipse can be used once in method
parameter list.
2.
Ellipse with type must be used in
parameter list at the end of the method
Example:
public int multiply(int... numbers){ int result = 1;
for(int number: numbers){
result= result*number; }
return result } }
6. Static import statement
Consider the java example: double r = Math.cos(Math.PI * theta);
How about writing the same java code like: double r = cos(PI *
theta); – looks more readable right?
This is where static import in java comes to help you.
import static java.lang.Math.PI;
import static java.lang.Math.cos;
JDK 1.6::named
Mustang released 11 Dec,2006
1. Scripting Language Support
2. JDBC 4.0 API
3. Java Compiler
4. Pluggable Annotations
5. Native PKI, Java GSS, Kerberos and LDAP support.
6. Integrated Web Services.
7. Lot more enhancements.
No comments:
Post a Comment