Hi, I am preparing for the
SCJP and is currently going trough
a book by Kathy Sierra and Bert Bates, "Sun Certified Programmer & Developer for
Java 2".
In "Chapter 3: Operators and Assignments" there is an example like this:
System.out.println("5.0 == 5L? " + (5.0 == 5L));
which produces the following output:
5.0 == 5L? true
It is stated to compare a floating point to an int!
First question is this really true?
As I have understood it a primitive value of 5.0 is default
a double (64 bit integer) and 5L is a long which means it also is a
64 bit integer and not an 'int' in strict Java terms.
I can understand the fact that the comparison results in true but
then when I tested something like: 5.0 == (byte)5
it still evaluates to true!??
Are there not any constraints in Java when comparing primitives of
different types, e.g a double and a byte ?
I can understand if byte,char and int (and maybe also a long) can
be compared due to Java's propagation to int (32-bit?) but how
is it possible to compare a float and/or double with a standard
int? If I can recall correctly that is not even possible in
e.g. C/C++. Yes, I know Java is not C/C++, but in this case I am
confused about what rules Java is applying. Hence, the very
restrict way assignment like: byte a = 1; a = a + a; is! It
returns with "cannot convert from int to byte"
So, I am somewhat confused why comparison is not as strict as
assignment or is it so simple that I have just misunderstood
the whole thing?