Type casting ambiguity in Java
There are no typecasts in the code you posted.
The following code doesn't compile :
byte a = 10;
byte b = 20;
byte c = a + b;
Compilation error -> Type mismatch: cannot convert from int to byte
Its a compilation error since :
All integer literals are treated as Integers in Java.
No it isn't. It's a compilation error because all operations between types narrower than int
produce int
values. JLS #4.2.2 Integer Operations:
If an integer operator other than a shift operator has at least one operand of type long, then the operation is carried out using 64-bit precision, and the result of the numerical operator is of type long. If the other operand is not long, it is first widened (§5.1.5) to type long by numeric promotion (§5.6).
Otherwise, the operation is carried out using 32-bit precision, and the result of the numerical operator is of type int. If either operand is not an int, it is first widened to type int by numeric promotion.
If what you claim as a Java rule really was a Java rule, all three lines would have failed to compile. But there is no such rule in Java as the one you claim. You just made it up.
So, a+b results in an integer literal
No it doesn't. It results in an integer value.
which needs to be typecasted to byte before storing in a byte type variable since there might be a loss of precision. I completely understands the above concept.
No, you don't understand it at all.
What confuses me is a similar concept with float variables. The below code snippet compiles successfully:
float d = 1.2;
float e = 2.3;
No it doesn't. Both lines produce compilation errors: error: incompatible types: possible lossy conversion from double to float
.
float f = d + e;
This compiles.
According to Java :
All decimal literals are treated as Decimal in Java.
There is no such rule. Again you have just made this up. There is no such thing as a 'decimal literal' in Java, or Decimal
either.
So, on similar note (d + e) must result in a decimal literal
No. It results in a float
value.
which will treated as decimal type
There is no 'decimal type' in Java. There is BigDecimal
, which is a class, not a type, but it doesn't relate to this question in any way.
and is getting stored in a float type variable. Here also we have a loss of precision.
No we don't. JLS #4.2.4 'Floating-Point Operations:
If at least one of the operands to a numerical operator is of type double, then the operation is carried out using 64-bit floating-point arithmetic, and the result of the numerical operator is a value of type double. If the other operand is not a double, it is first widened (§5.1.5) to type double by numeric promotion (§5.6).
Otherwise, the operation is carried out using 32-bit floating-point arithmetic, and the result of the numerical operator is a value of type float. (If the other operand is not a float, it is first widened to type float by numeric promotion.)
You wrote:
Why compiler doesn't force us here for explicit typecasting as in earlier case? Why is it not a compilation error here?
Because it isn't an error. It isn't an instance of the previous error because it doesn't involve types narrower than int
.
Don't just make rules up and then ask why Java doesn't comply with them. Check the actual rules.