-2

I am pretty new to Java. Came across a problem and would like to gain more insight on it.

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.

So, a+b results in an integer literal 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.

What confuses me is a similar concept with float variables. The below code snippet compiles successfully:

float d = 1.2;
float e = 2.3;
float f = d + e;

According to Java :

All decimal literals are treated as Decimal in Java.

So, on similar note (d + e) must result in a decimal literal which will treated as decimal type and is getting stored in a float type variable. Here also we have a loss of precision. Why compiler doesn't force us here for explicit typecasting as in earlier case ? Why is it not a compilation error here ?

Vijay
  • 117
  • 1
  • 9
  • 4
    It's not about the literals; it's about the `+` operator. `byte + byte` gives `int`, but `float + float` gives `float`. – Dawood ibn Kareem Jun 21 '17 at 02:46
  • 1
    Have a look at this [question](https://stackoverflow.com/questions/18483470/is-addition-of-byte-converts-to-int-is-because-of-java-language-rules-or-because). Possible Duplicate. – Charles Jun 21 '17 at 02:48
  • 1
    [Binary Numeric Promotion](https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.6.2) – Dawood ibn Kareem Jun 21 '17 at 02:49
  • 1
    Possible duplicate of [Is addition of byte converts to int is because of java language rules or because of jvm?](https://stackoverflow.com/questions/18483470/is-addition-of-byte-converts-to-int-is-because-of-java-language-rules-or-because) – sazzad Jun 21 '17 at 02:52
  • 1
    The 'according to Java' rules you have cited in quotes do not exist. You have just made them up. Don't use quote formatting for text that isn't quoted. – user207421 Jun 21 '17 at 04:19

2 Answers2

1

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.

user207421
  • 305,947
  • 44
  • 307
  • 483
0

In java you can't declare float values as :

float d = 1.2;
float e = 2.3;

Because compiler infers 1.2 and 2.3 as double. You have to cast them to float:

float d = (float) 1.2;
float e = (float) 2.3;

or

float d = 1.2f;
float e = 2.3f;
user207421
  • 305,947
  • 44
  • 307
  • 483
Jay Smith
  • 2,331
  • 3
  • 16
  • 27