Tiger Tiger Burning bright.....
Does the title speak to itself, well it was intended to, but if its not, read through so that it makes sense.
Java came with its version 5.0 long back, and the advantages of the same are posted everywhere in the web. Thought, Let me also put my contribution :-)
I was working with the earlier versions of java [1.42] until i joined my new company in July 07. I realized the power of tiger only when i literally starting doing development using Java 5.0 The main concepts i would like to share are
1. Java Generics
2. Advanced features of Collections
The items in the list are of utmost concern to us, the developers. Java 5.0 has made developers life lot more easier.
Java Generics
By introducing the java generics, the developers now are not required to do the class typecasting at each and every point. An example would me more apt.
Here is a typical usage of that sort:
List myIntList = new LinkedList(); // 1
myIntList.add(new Integer(0)); // 2
Integer x = (Integer) myIntList.iterator().next(); // 3
The cast on line 3 is slightly annoying. Typically, we know what kind of data has been placed into a particular list. However, the cast is essential. The compiler can only guarantee that an Object will be returned by the iterator. To ensure the assignment to a variable of type Integer is type safe, the cast is required.
Of course, the cast not only introduces clutter. It also introduces the possibility of a run time error, since we are prone to do mistakes. What if we could actually express our intent, and mark a list as being restricted to contain a particular data type? This is the core idea behind generics.
Here is a version of the program fragment given above using generics:
List myIntList = new LinkedList(); // 1
myIntList.add(new Integer(0)); //2
Integer x = myIntList.iterator().next(); // 3
Notice the type declaration for the variable myIntList. It specifies that this is not just an arbitrary List, but a List of Integer, written List. We say that List is a generic interface that takes a type parameter - in this case, Integer. We also specify a type parameter when creating the list object. The other thing to pay attention to is that the cast is gone on line 3. Now, you might think that all we’ve accomplished is to move the clutter around. Instead of a cast to Integer on line 3, we have Integer as a type parameter on line 1.
However, there is a very big difference here. The compiler can now check the type correctness of the program at compile-time. When we say that myIntList is declared with type List, this tells us something about the variable myIntList, which holds true wherever and whenever it is used, and the compiler will guarantee it.
Here is a typical usage of that sort:
List myIntList = new LinkedList(); // 1
myIntList.add(new Integer(0)); // 2
Integer x = (Integer) myIntList.iterator().next(); // 3
The cast on line 3 is slightly annoying. Typically, we know what kind of data has been placed into a particular list. However, the cast is essential. The compiler can only guarantee that an Object will be returned by the iterator. To ensure the assignment to a variable of type Integer is type safe, the cast is required.
Of course, the cast not only introduces clutter. It also introduces the possibility of a run time error, since we are prone to do mistakes. What if we could actually express our intent, and mark a list as being restricted to contain a particular data type? This is the core idea behind generics.
Here is a version of the program fragment given above using generics:
List myIntList = new LinkedList(); // 1
myIntList.add(new Integer(0)); //2
Integer x = myIntList.iterator().next(); // 3
Notice the type declaration for the variable myIntList. It specifies that this is not just an arbitrary List, but a List of Integer, written List. We say that List is a generic interface that takes a type parameter - in this case, Integer. We also specify a type parameter when creating the list object. The other thing to pay attention to is that the cast is gone on line 3. Now, you might think that all we’ve accomplished is to move the clutter around. Instead of a cast to Integer on line 3, we have Integer as a type parameter on line 1.
However, there is a very big difference here. The compiler can now check the type correctness of the program at compile-time. When we say that myIntList is declared with type List, this tells us something about the variable myIntList, which holds true wherever and whenever it is used, and the compiler will guarantee it.
The net effect, especially in large programs, is improved readability and robustness.
The next interesting thing we are going to discuss about, is the collections. The collection interface has been improved a lot and assists us in speeding up the development process.
Advanced features of Collections
Enhance For Loops
In previous versions of Java, in order to iterate through a collection of objects, you may of had to use the java.util.operator class, using code similar to the following:
for (Iterator i= list.iterator(); i.hasNext();)
{
Object listElement = i.next();
System.out.println((String)listElement);
}
You can see from the above example that the listElement is not strongly typed, and also, the code itself lacks elegance. In Java Tiger, you can use a for/in loop thus:
for (String listElement : list )
{
System.out.println(listElement);
}
Maps - A neat and tidy stuff
They have introduced a concept of entrySet in Maps. When we try to iterate through the Maps, our usual way of doing it would be
Map entryMap = new HashMap() // line 1
entryMap.put("1","1"); // line 2
entryMap.put("2","2"); // line 3
entryMap.put("3","3"); // line 4
entryMap.put("4","4"); // line 5
Iterator itKeys = entryMap.keySet().iterator(); // line 6
while(itKeys.hasNext()){ // line 7
String key = itKeys.next(); // line 8
String Value = entryMap.get(key); // line 9
} // line 10
If you see the above code, we get all the keys from Map (line 6) and iterate through each key, and to get the corresponding value, we do a .get(), passing in the key (line 9). This is another loop which happens in the map.
With the latest introduction of entryset in the java 5.0, our code will be so simple
IteratoritKeys = entryMap.entrySet().iterator(); // line 1
while(itKeys.hasNext()){ // line 2
Map.Entry sEntry = (Map.Entry) itKeys.next(); // line 3
String key = sEntry.getKey(); // line 4
String Value = sEntry.getValue(); // line 5
} // line 6
If you see, we get the whole key & value together in the form of Entry object (line 3) . The Map.entrySet method returns a collection-view of the map, whose elements are of this class. The only way to obtain a reference to a map entry is from the iterator of this collection-view. These Map.Entry objects are valid only for the duration of the iteration; more formally, the behavior of a map entry is undefined if the backing map has been modified after the entry was returned by the iterator, except through the setValue operation on the map entry.
More to come......
Java came with its version 5.0 long back, and the advantages of the same are posted everywhere in the web. Thought, Let me also put my contribution :-)
I was working with the earlier versions of java [1.42] until i joined my new company in July 07. I realized the power of tiger only when i literally starting doing development using Java 5.0 The main concepts i would like to share are
1. Java Generics
2. Advanced features of Collections
The items in the list are of utmost concern to us, the developers. Java 5.0 has made developers life lot more easier.
Java Generics
By introducing the java generics, the developers now are not required to do the class typecasting at each and every point. An example would me more apt.
Here is a typical usage of that sort:
List myIntList = new LinkedList(); // 1
myIntList.add(new Integer(0)); // 2
Integer x = (Integer) myIntList.iterator().next(); // 3
The cast on line 3 is slightly annoying. Typically, we know what kind of data has been placed into a particular list. However, the cast is essential. The compiler can only guarantee that an Object will be returned by the iterator. To ensure the assignment to a variable of type Integer is type safe, the cast is required.
Of course, the cast not only introduces clutter. It also introduces the possibility of a run time error, since we are prone to do mistakes. What if we could actually express our intent, and mark a list as being restricted to contain a particular data type? This is the core idea behind generics.
Here is a version of the program fragment given above using generics:
List
myIntList.add(new Integer(0)); //2
Integer x = myIntList.iterator().next(); // 3
Notice the type declaration for the variable myIntList. It specifies that this is not just an arbitrary List, but a List of Integer, written List
However, there is a very big difference here. The compiler can now check the type correctness of the program at compile-time. When we say that myIntList is declared with type List
Here is a typical usage of that sort:
List myIntList = new LinkedList(); // 1
myIntList.add(new Integer(0)); // 2
Integer x = (Integer) myIntList.iterator().next(); // 3
The cast on line 3 is slightly annoying. Typically, we know what kind of data has been placed into a particular list. However, the cast is essential. The compiler can only guarantee that an Object will be returned by the iterator. To ensure the assignment to a variable of type Integer is type safe, the cast is required.
Of course, the cast not only introduces clutter. It also introduces the possibility of a run time error, since we are prone to do mistakes. What if we could actually express our intent, and mark a list as being restricted to contain a particular data type? This is the core idea behind generics.
Here is a version of the program fragment given above using generics:
List
myIntList.add(new Integer(0)); //2
Integer x = myIntList.iterator().next(); // 3
Notice the type declaration for the variable myIntList. It specifies that this is not just an arbitrary List, but a List of Integer, written List
However, there is a very big difference here. The compiler can now check the type correctness of the program at compile-time. When we say that myIntList is declared with type List
The net effect, especially in large programs, is improved readability and robustness.
The next interesting thing we are going to discuss about, is the collections. The collection interface has been improved a lot and assists us in speeding up the development process.
Enhance For Loops
In previous versions of Java, in order to iterate through a collection of objects, you may of had to use the java.util.operator class, using code similar to the following:
for (Iterator i= list.iterator(); i.hasNext();)
{
Object listElement = i.next();
System.out.println((String)listElement);
}
You can see from the above example that the listElement is not strongly typed, and also, the code itself lacks elegance. In Java Tiger, you can use a for/in loop thus:
for (String listElement : list )
{
System.out.println(listElement);
}
Maps - A neat and tidy stuff
They have introduced a concept of entrySet in Maps. When we try to iterate through the Maps, our usual way of doing it would be
Map
entryMap.put("1","1"); // line 2
entryMap.put("2","2"); // line 3
entryMap.put("3","3"); // line 4
entryMap.put("4","4"); // line 5
Iterator
while(itKeys.hasNext()){ // line 7
String key = itKeys.next(); // line 8
String Value = entryMap.get(key); // line 9
} // line 10
If you see the above code, we get all the keys from Map (line 6) and iterate through each key, and to get the corresponding value, we do a .get(), passing in the key (line 9). This is another loop which happens in the map.
With the latest introduction of entryset in the java 5.0, our code will be so simple
IteratoritKeys = entryMap.entrySet().iterator(); // line 1
while(itKeys.hasNext()){ // line 2
Map.Entry
String key = sEntry.getKey(); // line 4
String Value = sEntry.getValue(); // line 5
} // line 6
If you see, we get the whole key & value together in the form of Entry object (line 3) . The Map.entrySet method returns a collection-view of the map, whose elements are of this class. The only way to obtain a reference to a map entry is from the iterator of this collection-view. These Map.Entry objects are valid only for the duration of the iteration; more formally, the behavior of a map entry is undefined if the backing map has been modified after the entry was returned by the iterator, except through the setValue operation on the map entry.
More to come......
Comments