File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes comparisons Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "comparisons " Watch "comparisons " New topic


akmal jah
Ranch Hand

Joined: Feb 18, 2002
Posts: 31
public class Test041
static double d;
static float f;
public static void main(String args[])
Integer.MAX_VALUE );
Short.MAX_VALUE );
static void compare(double d, float f)
if (f == d) System.out.print("
equal" );
else System.out.print("

Answer is : equal unequal equal equal equal
can somebody help me understand this and is there any tricks to remember results of such comparisons ?
Bishal P
Ranch Hand

Joined: Sep 06, 2002
Posts: 43
The character, short and byte dont need any explanation.
However the int behaviour was very strange so i executed the program with some debugs.
The Long.Max_VALUE is 9223372036854775807 which is 9.223372036854776E18 in double and 9.223372E18 in float.
The Integer.MAX_VALUE is 2147483647 which is 2.147483647E9 in double and 2.14748365E9 in float. As you can the number is rounded of in float. So the compariosion returned "unequal"
I hope they dont give such questions in the real exam. coz if they did then god help me

_ _____ _ <br />Used to be a Java Programmer but now I work on Microsoft Technologies - Word, Excel and Outlook!
I agree. Here's the link:
subject: comparisons
It's not a secret anymore!