java - Why does this cause a ClassCastException? -


public class test8 {    public static void main (string args[]) {       number numberref = new integer(0);       double doubleref = (double)numberref;    } } 

it shows me exception @ runtime:

 exception in thread "main" java.lang.classcastexception:   java.lang.integer cannot cast java.lang.double 

why so?

you trying cast instance of integer reference double cannot happen. integer , double 2 different classes , objects of each cannot cast each other

number common super class integer , double, hence instance of double or integer can upcasted number. in other words, integer is a number, double is a number but, integer is not a double

integer = new integer(0); double d = new double(1); number ni = i; number di = d; integer id = d; //invalid; double dd = i; //invalid 

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -