Unchecked Cast in Java

Rupam Yadav Nov 30, 2021
  1. What Is the Unchecked Cast Warning in Java
  2. Ignore the Warnings Using @SuppressWarnings
Unchecked Cast in Java

Java is a programming language that enforces type safety which means that we should always specify the type of data that we are going to store or use and cannot store incompatible types in them.

For example, we cannot store an Integer value in a String, and the compiler will throw an error or a warning. One of the warnings related to data types is unchecked cast.

What Is the Unchecked Cast Warning in Java

The unchecked cast warning occurs when we try a raw type to a parameterized type without checking its type. Java does not encourage this method because a parameterized type is restricted to a specified type only.

One of the parameterized types in Java is HashMap, which takes two parameters, the key type, and the value type. In the example code below, we create an object of HashMap called rawMap and put some values with their keys in it.

When we compile this program, a few warnings show up, like the ones shown below. The first two warnings occur because we use Map and HashMap parameterized classes as raw.

The last three warnings are for every individual put statement in the program because we put values without specifying their types or checking the type; thus, the unchecked cast warning is coming up.

import java.util.HashMap;
import java.util.Map;

public class Main {

    public static void main(String[] args) {
        Map rawMap = new HashMap();
        rawMap.put("key1", "String1");
        rawMap.put("key2", "String2");
        rawMap.put("key3", "String3");

        System.out.println(rawMap);
    }
}

Output:

{key1=String1, key2=String2, key3=String3}

Warnings:

Raw use of parameterized class 'Map'
Raw use of parameterized class 'HashMap'
Unchecked call to 'put(K, V)' as a member of raw type 'java.util.Map'
Unchecked call to 'put(K, V)' as a member of raw type 'java.util.Map'
Unchecked call to 'put(K, V)' as a member of raw type 'java.util.Map'

Now that we know the actual problem, we can discuss the solutions or workarounds to this problem.

Ignore the Warnings Using @SuppressWarnings

We can use the @SupressWarnings annotation if we want to ignore the warnings. This is a good solution, but only when we know that there will be no issues regarding this warning in the future.

To use this annotation, we have passed the warning that we want to suppress, which is “unchecked”. We use this annotation on the function from which the warning is generating.

Below is the same program we see in the previous example, but we use the @SuppressWarnings annotation on the main() function and pass “unchecked " in it. Notice that the HashMap is still being used as a raw type, but the warnings are gone when we compile it.

import java.util.HashMap;
import java.util.Map;

public class Main {

    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        Map rawMap = new HashMap();
        rawMap.put("key1", "String1");
        rawMap.put("key2", "String2");
        rawMap.put("key3", "String3");

        System.out.println(rawMap);
    }
}

Output:

{key1=String1, key2=String2, key3=String3}

Warnings:

Raw use of parameterized class 'Map'
Raw use of parameterized class 'HashMap'

Another obvious solution to this issue is to use the parameterized type HashMap as it is supposed to be. In the following example, we specify the key’s type and value in the HashMap, and the warnings are gone.

import java.util.HashMap;
import java.util.Map;

public class Main {


    public static void main(String[] args) {
        Map<String, String> rawMap = new HashMap<>();
        rawMap.put("key1", "String1");
        rawMap.put("key2", "String2");
        rawMap.put("key3", "String3");

        System.out.println(rawMap);
    }
}

Output:

{key1=String1, key2=String2, key3=String3}
Author: Rupam Yadav
Rupam Yadav avatar Rupam Yadav avatar

Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.

LinkedIn