Is type casting possible between wrapper classes in Java? -
is type casting possible between wrapper classes in java?
the code here tried:
public class test { public static void main(string[] args) { double d = 100.04; long l = (long) d.doublevalue(); //explicit type casting required int = (int) l; //explicit type casting required system.out.println("double value " + d); system.out.println("long value " + l); system.out.println("int value " + i); } }
why isn't long
casted int
in program?
long l = (long)d.doublevalue(); //explicit type casting required int = (int)l; //not valid, because here casting wrapper long
in above line, casting of l not possible int because wrapper classes autoboxing , unboxing. refer: https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html. wrapper class casted corresponding primitive type.
long l = (long)d.doublevalue(); //explicit type casting required int = (int)l; //valid, because here casting primitive primitive
in above lines casting of l possible int because long primitive type , int primitive type. here primitive narrowing casting take place casting long int may result in loss of precision.
Comments
Post a Comment