Skip to content Skip to sidebar Skip to footer

Directly Casting Primitive Type To Object Is Fine In Java?

I am directly casting my int primitive type variable to Integer object like below: int intValue = 0; Integer value = (Integer) intValue; Is this fine or will cause some unexpected

Solution 1:

Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes.

intintValue=0;
Integervalue= intValue; // by Autoboxing

you can also convert it using valueOf method of wrapper class

Integer value = Integer.valueOf(intValue)

Check What code does the compiler generate for autoboxing? if you still have doubts.

Post a Comment for "Directly Casting Primitive Type To Object Is Fine In Java?"