Data Structures JDK 1.5 Features


Data Structures JDK 1.5 Features

Summary: This tutorial "Data Structures JDK 1.5 Features", narrates the new features added to DS with JDK 1.5

JDK 1.5 brought many changes in data structures. It not only added a new DS Queue but also added extra features to the existing collections framework.

Evolution of DS with JDK Versions
  • JDK 1.0 DS includes only 4 concrete classes Stack, Vector, Hashtable and Properties. These four DS are known as legacy classes.
  • JDK 1.2 brought many DS like ArrayList, TreeSet, LinkedList, HashMap etc. and also comes with two classes Collections and Arrays with which DS and array operations became very easy like searching, indexing and sorting etc. All these classes are known as collections framework.
  • JDK 1.4 added only one interface RandomAccess with which retrieval of elements is faster.
  • JDK 1.5 comes with a new interface Queue; but added many features to the existing DS like generics, autoboxing and enhanced for loop (known as foreach loop).
Data Structures JDK 1.5 Features

JDK 1.5 brought metamorphic changes in DS manipulation with which usage of DS became very easy compared to JDK 1.4 and earlier. The features are autoboxing, generics and enhanced for loop. Queue is added in JDK 1.5. Let us discuss one by one.

1. Autoboxing

Earlier to JDK 1.5, that is upto JDK1.4, the DS of Java do not accept data types. Following statements raise compilation error.

Vector vect = new Vector();
int x = 10;
vect.addElement(x);
double y = 10.5;
vect.addElement(y);

Then what the programmer followed to store x and y values? Convert them into object and then store. That is, upto JDK 1.4, the collections classes (all DS) stores only objects and returns objects. Following code works.

Vector vect = new Vector();
int x = 10;
Integer i1 = new Integer(x);
vect.addElement(i1);
double y = 10.5;
Double d1 = new Double(y);
vect.addElement(d1);

This is extra work for the programmer; converting data types into objects just for storage. While retrieving also, the same laborious work is to done. Observe.

Object obj = vect.elementAt(0);
Integer i2 = (Integer) obj;
int k = i2.intValue();

Similarly is with double also.

Object obj = vect.elementAt(1);
Double d2 = (Double) obj;
double m = d2.intValue();

Now you can use k and m in your code straightaway as int and double values. From JDK 1.5, the scenario completely changed. Following code works.

Vector vect = new Vector();
int x = 10;
vect.addElement(x);
double y = 10.5;
vect.addElement(y);

What is happening internally? Who has converted int x into Integer i1? Did the designers changed complete coding of their DS? No, the designers did not do much work over this. What you have done manually (converting data type into a wrapper object) earlier is done now by the designers internally. This feature is known as autoboxing. To put it more clearly, converting data type into object is known as autoboxing and converting object back into data type is known as unboxing. Autoboxing introduced with JDK 1.5 makes programmer job easy to use DS in Java.

5 thoughts on “Data Structures JDK 1.5 Features”

  1. manisha srivastava

    sir,,, plz tell me, what happens if we r not using the wrapper classes,,,, how it is helpful and why it is needed?

    1. Wrappers give a an object form for all data types. For example, Integer is just wrapper to int data type. Whenever you require an int as an object use, Integer object. For example, all the DS of Java requires an object to store. If you add, int to Vector it shows error. You must convert int to Integer and then store. This conversion is done from JDK 1.5 using autoboxling.

  2. PavanKumarreddy

    From the conclusion of auto boxing the designers did the convert ion of data type into object internally then what is the purpose of introducing the concept of wrapper classes to convert data type as object?

Leave a Comment

Your email address will not be published.