Java.lang.integer Cannot Be Cast To Java.lang.boolean

Java.lang.integer Cannot Be Cast To Java.lang.boolean | While writing Java programs ClassCastException is one of the common exceptions we get due to incompatible conversions. In this post, we will discuss java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.Boolean (java.lang.Integer and java.lang.Boolean are in module java.base of loader ‘bootstrap’).

Example-1: java.lang.integer cannot be cast to java.lang.boolean

public class Main {
    public static void main(String[] args) {
        Object object = Integer.valueOf(9);
        System.out.println(object);
        
        Boolean flag = (Boolean) object;
        System.out.println(flag);
    }
}

It gives the following output and exception:-

9
Exception in thread “main” java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.Boolean (java.lang.Integer and java.lang.Boolean are in module java.base of loader ‘bootstrap’)
at Main.main(Main.java:6)

The Object class of the java.lang class is the superclass of all Java classes therefore it can hold any reference. Hence it can also hold the value of java.lang.Integer class. But note that the internal object will be Integer only, and we can’t convert it in into any value which is incompatible.

The java.lang.Integer class is incompatible with java.lang.Boolean value. Therefore the object which is holding an integer internally can’t be cast to other wrapper classes. To convert java.lang.Integer value from object type to java.lang.Boolean, first we have to convert the object to an integer.

public class Main {
    public static void main(String[] args) {
        Object object = Integer.valueOf(9);
        System.out.println(object);

        // should not be used
        Boolean flag = Boolean.valueOf(String.valueOf(object));
        System.out.println(flag);
    }
}

Output:-

9
false

The above program doesn’t give the correct result because first, we are converting an object into the string type and then into the boolean type. The public static Boolean valueOf(String s) method of the Boolean class returns the true value if the string argument is not null and is equal, ignoring the case, to the string “true”. Otherwise, a false value is returned, including a null argument. Therefore in the above program, we will always get a false value.

public class Main {
    public static void main(String[] args) {
        Object object = Integer.valueOf(9);
        System.out.println(object);

        Boolean flag = ((Integer) object > 0) ? true : false;
        System.out.println(flag);
    }
}

Output:-

9
true

Example-2: java.lang.integer cannot be cast to java.lang.boolean

Let us see an another example where we will get java.lang.integer cannot be cast to java.lang.boolean.

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<Boolean> list = new ArrayList<>();
        list.add(false);
        list.add(true);
        list.add(true);
        System.out.println(list);

        Object object = Integer.valueOf(9);
        list.add((Boolean) object);
        System.out.println(list);
    }
}

Output:-

[false, true, true]
Exception in thread “main” java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.Boolean (java.lang.Integer and java.lang.Boolean are in module java.base of loader ‘bootstrap’)
at Main.main(Main.java:13)

In the above program, list is of Boolean type. When we try to add an object value that internally contains Integer by casting it into the Boolean type then JVM gives java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.Boolean.

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<Boolean> list = new ArrayList<>();
        list.add(false);
        list.add(true);
        list.add(true);
        System.out.println(list);

        Object object = Integer.valueOf(9);
        list.add(((Integer) object > 0) ? true : false);
        System.out.println(list);
    }
}

Output:-

[ false, true, true ]
[ false, true, true, true ]

Also see:- Exception Handling Interview Questions in Java, Java Exception Handling MCQ – 1, Catch All Exceptions in Java, Exception Propagation in Java

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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