Your problem is that you are trying to assign an int
to a long
which results in a ClassCastException
.
To get the correct result you will need to define the value 0
that you are trying to cast to a long
as an actuall long
value.
In that case every non floating number that you are trying to assign is actually handelt to be an int
if you don´t explicity define the type of the value.
This can easily be seen if you just write long i = 11111111111;
. Your compiler should tell you that the range for this number is out of the scope for an int
. To tell the compiler that this value should be handelt as a long you simply need to add an L
after the number: long i = 11111111111L;
.
In your case your statement should be
System.out.println((long)(new Object()!=null ? 0L : new Object()));
Some additional information:
A similiar scenario happens with the double
aswell. float f = 0.;
wont work because you would need to define it as a float by adding a F
after the value. float f = 0.F;
EDIT:
Your value 0
should get wrapped into Integer
because it is possible that it could return an Object
. if you write it as System.out.println((long)(new Object()!=null ? 0 : 1));
this would compile again.