casting - Java typecasting confusion -
this question has answer here:
take these 2 snippet example:
case 1:
scanner s = new scanner(system.in); int n = s.nextint(); /** take user input **/ n *= double.negative_infinity;
and
case 2:
int k=10; double kk = 10.10; int rst = k*kk;
in both cases, not doing typecasting side, case 1 executes , prints value of n
correctly case 2 throws error, can not convert double int
. why difference?
the first works , second doesn't because *=
+=
-=
etc add automatic cast.
if, example, change second example to;
int k=10; double kk = 10.10; k*= kk;
it should work. alternatively add explicit cast rst = (int)(k*kk);
Comments
Post a Comment