Generic Type Casting

Generic Type Casting

Generics is one of the most significant enhancements to java by J2SE 5.0 programming language. Generics eliminates the need of explicit type casting in collections. A collection is a set of interfaces and classes that sort and manipulate a group of data into a single unit.

To retrieve elements from a collection, we need to type cast the elements because each element in a collection is considered to be an object. Type casting is an unsafe operation because the compiler cannot check the wrong casts. The compiler throws an exception if casts fails at run time.

When using generics the compiler inserts type casts at appropriate places to implement type casting.  Therefore the type cast becomes implicit rather than explicit.

Generics also determines the cast errors at compile time rather than run time. Collections can contain collection of objects of any type.

Using generics we can specify the type information of data using a parameter. The type information specifies the class and hierarchy of classes and interface to which the object belongs.

The process of converting one type of object and variable into another type is referred to as Typecasting. When the conversion automatically performs by the compiler without the programmers interference, it is called implicit type casting or widening casting.

In implicit typecasting, the conversion involves a smaller data type to the larger type size. For example, the byte datatype implicitly typecast into shortcharintlongfloat, and double. The process of converting the lower data type to that of a higher data type is referred to as Widening.

When you assign value of one data type to another, the two types might not be compatible with each other. If the data types are compatible, then Java will perform the conversion automatically known as Automatic Type Conversion and if not then they need to be casted or converted explicitly. For example, assigning an int value to a long variable.

Widening conversion takes place when two data types are automatically converted. This happens when:

  • The two data types are compatible.
  • When we assign value of a smaller data type to a bigger data type.

 

Narrowing or Explicit Conversion

If we want to assign a value of larger data type to a smaller data type we perform explicit type casting or narrowing.

  • This is useful for incompatible data types where automatic conversion cannot be done.
  • Here, target-type specifies the desired type to convert the specified value to.

 

Program illustration of use of generic type in collections

Import java.util.*;

Public class arraylistcollection

{

Arraylist<ineteger> list = new arraylist<Integer>();

Numberinglist(list);

Int total = 0 ;

Iterator<integer>iter = list.iterator();

While ( iter.hasnext())

{

Integer val = iter.next();

Total = total + val;

}

System.out.println(“The total amount is “ + total );

Private static void numberinglist(arraylist<integer>list)

{

List.add(new Integer(1));

List.add(new Integer(2));

}

}

Leave Comment

Important Topics

Title
OOPS
Data Types
JAVA
JVM
Command Line Args
Machine Neutral
Scope of Variables
Operators :
Generic Type Casting
IF Else
Switch Statement
The while statement
The do statement
The for statement
Classes, objects and methods
Constructors
Methods Overloading
Static Members
Nesting of methods
Inheritance : Extending a Class
Overriding methods
Overriding methods
Final variable and methods
Abstract class in Java
Visibility control