Type-Safety and Type-Casting in Java Generics

Last Updated : 5 Aug, 2025

Java Generics enable you to write code that works with different data types while ensuring type safety at compile time.

Type-Safety

Arrays in Java are type-safe, meaning they can only store elements of a specific type. For example, a String[] can hold only String objects and adding any other type causes a compile-time error.

Example: Assigning a non-String to a String[] causes a compile-time error.

Java
import java.io.*;

class GFG {
    public static void main (String[] args) {
      String name[] =new String[500];
      name[0] = "Vivek Yadav";
      name[1] = "Ravi";
      name[2] = new Integer(100); 
    }
}

Output:

output

Explanation:

  • A String array named name with a size of 500 is created.
  • The first two elements ("Vivek Yadav" and "Ravi") are valid String assignments.
  • The third assignment tries to store an Integer in a String array, which causes a compile-time error.
  • This example shows that arrays are type-safe and only allow elements of the declared type.
  • Any attempt to store a different type results in a compile-time error, ensuring type safety.

It is not recommended to use a ArrayList without generics. If you accidentally add a different type, it won't cause a compile-time error, but the program may fail at runtime.

Example: Showing lack of type safety in non-generic ArrayList, requires explicit type casting and may cause runtime errors

Java
import java.io.*;
import java.util.*;
class GFG {
    public static void main (String[] args) {
      ArrayList al =new ArrayList();
      al.add("Vivek Yadav");
      al.add("Ravi");
      al.add(new Integer(10));

      String name1 = (String)al.get(0);
      String name2 = (String)al.get(1);
      String name3 = (String)al.get(2);
    }
}

Output:

output

Explanation:

  • The ArrayList is non-generic, so it accepts any type of object.
  • It stores two strings and one integer.
  • Casting all elements to String causes a runtime error (ClassCastException) when casting the integer.
  • This shows non-generic collections are not type-safe.
  • It's recommended to use generics to catch such errors at compile time.

2. Type-Casting

In the case of the array at the time of retrieval, it is not required to perform any type casting. 

Java
import java.io.*;
import java.util.*;

class GFG {
    public static void main(String[] args)
    {
        String name[] = new String[500];
        name[0] = "Vivek Yadav";
        name[1] = "Ravi";
        name[2] = new Integer(100);
    }
}

Output:

output

Explanation: Here type casting is not required. But in the case of collection at the time of retrieval compulsory, we should perform type casting otherwise we will get compile time error.

Example: Program to retrieves the first element, but no type is specified.

Java
import java.io.*;

class GFG {
    public static void main (String[] args) {
        ArrayList al =new ArrayList();
        al.add("Vivek Yadav");
        al.add("Ravi");
        String name1= al.get(0);
    }
}

Output:

output

Explanation:

  • A raw (non-generic) ArrayList is created and stores two String values.
  • The method al.get(0) retrieves the first element, but no type is specified.
  • Assigning it directly to a String causes a compile-time error due to missing type cast.

Example:

ArrayList<String> al = new ArrayList<String>();

al.add("Vivek Yadav");

al.add(10); // Compile-time error: incompatible types

Explanation:

  • Using Generics provides type safety, this ArrayList can store only String objects.
  • Trying to add any other type (like int) results in a compile-time error.

At the time of retrieval, there's no need for type casting.

String name = al.get(0); // Safe and direct assignment

Example: Retrieve Arraylist element without type casting.

Java
import java.io.*;

class GFG {
    public static void main (String[] args) {
      ArrayList<String> al =new ArrayList<String>();
      al.add("GFG");
      String name =al.get(0);
      System.out.println(name);
    }
}

Output:

output

Explanation: Type casting is not required as it is a TypeSafe. That is through generic syntax we can resolve type casting problems.

Comment