In the SCJP leadership book by Katie Sierra, in the assignment chapter, we learn that we can declare something like this byte b = 7;. Behind the scene code byte b = (byte) 7;. This is because in java the number 7 is considered the literal value of int, so it needs to be converted to int.
Now the situation is different. A double can include every byte contained within the float value, since it is a larger data type. So, we can say that float f = 10.543;As 10.543 is a rather small value and should fit into the float. Also, the literal value for such a number is considered Double, so the compiler must implicitly use it for float. But this is not so, the compiler stops us. We must add For Fafter this value.
Why do these two conflicting behaviors exist for literal assignment of values? In short, if byte b = 7possible. Why is float f = 10.543it impossible?
source
share