Explore Courses Blog Tutorials Interview Questions
0 votes
in Java by (7k points)

I know type casting is done automatically in Java for lower precision primitive type to higher precision. For example in this code:

int i = 20;

int j = 40;

float k = i + j;

What actually happens internally? In the third statement, do the values of i and j individually get casted to float and then added together?

Or the addition is done first in int type and then the result of the addition is casted into float?

1 Answer

0 votes
by (13.1k points)

They are added as ints and then cast into float because of the assignment. However, if you executed float k = (float) i + j then j would be cast to float because the left-hand side of the addition is a float. Same for float k* 1.0f*i+ j

Want to learn Java? Check out the Java certification from Intellipaat.

Related questions

0 votes
1 answer
asked Jan 21, 2020 in Java by angadmishra (6.5k points)
0 votes
1 answer
asked Mar 17, 2021 in Java by Jake (7k points)
0 votes
1 answer
asked Jul 15, 2019 in Java by Nigam (4k points)

Browse Categories