Hi all,

I do some testing on casting and confused with followings. First check the given code.

Code 1,

Code:
int x = 10;
float y = 15.2f;
int z = x + (int)y;
System.out.println(z);
Code 2,

Code:
int x = 10;
float y = 15.2f;
System.out.println(x + y);
Code 3,

Code:
int x = 10;
float y = 15.2f;
int z = x + y;
System.out.println(z);
My explanations on these are as follows.

On code one try to printout z, is an int type. So that it’s need to casting float into int. Result is 25. It’s ok.

On code two result is 25.2 because on println() method if any variable in a group of concatenated variables is a string, Java gets it as a single String.

On the last code I’m confusing. It is wrong. Why it is not possible automatic casting since int and float are 32bits long.

Can you guys make a note on this.
Thanks.