• 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
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

tricky java question

 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

one of my friend asked this question .i am struggling


What makes the following code print false?
 
Sheriff
Posts: 22716
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll give you a hint. The type is either float or double, and you can find some information in java.lang.Float and java.lang.Double.
 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure that it gives flase..??
I just run the program now... It give true


public class Simply
{
public static void main(String[] args)
{
float x = 2.0f;
System.out.println(x == x);
}
}
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'll need to fill in something different than 2.0f for the value. See the API documentation of java.lang.Float, that might give you an idea...
 
Marshal
Posts: 77214
370
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Rob has hinted, there is in fact a float value that would work for, but it's not 2.0f. There are more hints in the Java Language Specification, but the JLS is heavy reading.
 
Bartender
Posts: 4109
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it



[ August 29, 2008: Message edited by: Vijitha Kumara ]
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vijitha Kumara ,

good. can you explain in details that why it is giving false?..

 
Vijitha Kumara
Bartender
Posts: 4109
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JLS says:

A NaN value is used to represent the result of certain invalid operations such as dividing zero by zero.



May be It's the way java allocate the variable (with the NaN constant). May be Someone has a better idea(actual reason) Campbell/Rob Prime/Jesper?

[ August 29, 2008: Message edited by: Vijitha Kumara ]
[ August 29, 2008: Message edited by: Vijitha Kumara ]
 
Campbell Ritchie
Marshal
Posts: 77214
370
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Go to the Java Language Specification link I provided earlier and look for �4.2.3 and �4.2.4.

You can get NaN from various operations; 0f / 0f or Math.sqrt(-1) will both give Nan. In the case of the sqrt method (which is actually written in C or C++) there is something in it which in Java would be like this:That acts as a guard against trying to calculate the square root of 0 (which is 0) or of a negative number which is not-a-number.

In the Float API documentation it says that NaN is not ordered; that becomes obvious when we try some arithmetic.

2 * 0 = 0
3 * 0 = 0
∴ 2 * 0 = 3 * 0
Multiply both sides by 0 / 0, which is Nan; that gets rid of the zeroes
∴ 2 = 3

The only way you can get out of this nonsense result is saying that NaN is not equal to itself.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Campbell Ritchie:
2 = 3

The only way you can get out of this nonsense result is saying that NaN is not equal to itself.




Thanks Cambpell and vijitha

 
Campbell Ritchie
Marshal
Posts: 77214
370
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic