I confused with the error when i wrote the code like short a = 1;a=a (short)1; [closed]

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;

Leave a Comment