Data Structures JDK 1.5 Features

Data Structures JDK 1.5 Features

2. Generics

A general data structure created gives a compilation warning with JDK 1.5

Vector vect = new Vector();

To the above vect object any Java data can be added as follows.



The vect object stores int, double, boolean and string values. This is fine, no problem if the programmer would like to add any data type or object. If the programmer would like to add only integers or strings, he must check explicitly with some validation code. To avoid this extra validation code, the JDK 1.5 introduced a concept called generics. In generics, the validation code is written by the data structure itself.

Vector vect1 = new Vector();

The above Vector object vect1 stores only integer values. If other added, it is compilation error.

vect1.add(10); // works fine
vect1.add(10.5); // compilation error
vect1.add("hello"”"); // compilation error

Let us see another data structure ArrayList that stores only strings.

ArrayList al = new ArrayList();

al.add(“hello”); // works fine
al.add(10); // compilation error
al.add(10.5); // compilation error

Creating a data structure to accept only one type data is known as generics. Generics give type-safe operation. If the programmer designs a non-generic data structure, the compiler displays a warning "unchecked and unsafe operation". Finally to say, generics allow storing only one type of data and this avoids validation code. Generics make implicitly checked and safe operation.

3. Enhanced for loop

Earlier before JDK 1.5, programmer write a for loop to print the elements of a data structure.

The above for loop known as basic for loop includes incrementing and condition checking. To avoid this, to print the values of a data structure or array, JDK 1.5 comes with a simple for loop known as enhanced for loop or foreach loop. This avoids incrementing and condition checking.

For every iteration, one element of vect is copied into k and k is printed.

To discuss very clearly about this new loop, two programs are given using with arrays and data structures.

Arrays Enhanced for loop (foreach)
Collections Enhanced for loop (foreach)

Pass your suggestions to imporve the quality of this tutorial "Data Structures JDK 1.5 Features".

5 thoughts on “Data Structures JDK 1.5 Features

    1. S. Nageswara Rao, Corporate Trainer Post author

      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.

  1. 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 Reply

Your email address will not be published. Required fields are marked *