Question

Today when i wrote some java codes it always gives me tips of error in red colors. Flowing is the code:

short a = 1;a=a + (short)1;

I guess it must be the ClassCastException,but i am not able to understand the reason,could you do me a favor?Thanks!

Était-ce utile?

La solution

You can better try with:

a= (short)1;

instead of

a=a (short)1;

ClassCastException is:-

Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance.

Although in your code it is not making sense how you receive the ClassCastException even after if we change as I suggested.

As in your code you have taken a as short and initialized it with value 1. Now later why you want to cast the value a as short as it is already short?

Example of ClassCastException could be like this:-

Object i = Integer.valueOf(1);
String s = (String)i;

Autres conseils

a=a (short)1; is wrong syntax.

The correct syntax would be

a= (short)1;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top