This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why it is false Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "why it is false" Watch "why it is false" New topic
Author

why it is false

Naveen Sharma
Ranch Hand

Joined: Mar 23, 2001
Posts: 65
public class Test
{
public static void main(String[] args)
{
double d = Integer.MAX_VALUE;
float f= Integer.MAX_VALUE;
if (f == d)
System.out.println("equal");
else
System.out.println(" not equal ");
}
}
it prints not equal however if i take float f = 10; and double d =10 its shows equal
please explain
thanks
NAveen
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Originally posted by Naveen Sharma:
public class Test
{
public static void main(String[] args)
{
double d = Integer.MAX_VALUE;
float f= Integer.MAX_VALUE;
if (f == d)
System.out.println("equal");
else
System.out.println(" not equal ");
}
}
it prints not equal however if i take float f = 10; and double d =10 its shows equal
please explain
thanks
NAveen

Add lines:

which should give you an idea why...
------------------
Antti Barck
It Solutions Consultant, NSD Oy
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12268
    
    1
Although both float and int use 32 bits, float has to use some bits for the exponent, therefore it can't maintain the low significance bits in Integer.MAX_VALUE. However, double with 64 bits to play with can.
Bill
------------------
author of:


Java Resources at www.wbrogden.com
tvs sundaram
Ranch Hand

Joined: Jan 28, 2001
Posts: 153
In simple terms double becomes more precise with 64 bit info than that of float with 32 bit.
Hence whenever u do a calculation on RHS and assign the same to double and float they won't be equal.
Java Gurus pls correct me if the interpretation is wrong.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why it is false
 
Similar Threads
using ==
Wrapper question...
equals
Confusing outputs
About primitive promotion.