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

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

angularjs - ng-repeat duplicating items after page reload -