• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

comparisons

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Test041
{
static double d;
static float f;
public static void main(String args[])
{
compare(Long.MAX_VALUE,
Long.MAX_VALUE );
compare(Integer.MAX_VALUE,
Integer.MAX_VALUE );
compare(Character.MAX_VALUE,
Character.MAX_VALUE);
compare(Short.MAX_VALUE,
Short.MAX_VALUE );
compare(Byte.MAX_VALUE,
Byte.MAX_VALUE );
}
static void compare(double d, float f)
{
if (f == d) System.out.print("
equal" );
else System.out.print("
unequal");
}
}

Answer is : equal unequal equal equal equal
can somebody help me understand this and is there any tricks to remember results of such comparisons ?
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic