This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Tricky Question...? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Tricky Question...?" Watch "Tricky Question...?" New topic
Author

Tricky Question...?

santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296

What makes the following code print false ?


Creativity is nothing but Breaking Rules
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14196
    
  20

Well, what do you think? Do you have an idea yourself?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

One hint: <sometype> is a primitive.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 605

Well I am assuming that in the question I am only allowed to fill in 'sometype' and 'somevalue' and not make any more changes.

Well in which case whatever answer you give me is going to shake my concepts on java. A '==' returning false that's pretty big!

@Rob - We'll with your hint I still don't work this out. If you meant boolean I don't see how it works.

I give up. Is this even really possible


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

I give up. Is this even really possible


Of course, it's possible. It is a trick question, after all.

And like all trick questions, either you know it or you don't.... but as another hint.... It is related to floating points. And related to how IEEE defines how equality works with floating points.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
i''m not having Idea
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

also it is already discussed here and Rob answared this question ... search here
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 605

seetharaman venkatasamy wrote:also it is already discussed here and Rob answared this question ... search here


Exactly what phrase do you want us to search with?
Rob's answered almost every question out here so searching by his name is not a viable option!

@Henry - Point taken - Actually I did think about floating point numbers. I know them all too well.
But to say x == x gives a false -- I cannot think of any possible value of x where this happens? So what could the special value be.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Sam Mercs wrote:
Exactly what phrase do you want us to search with?


i simply search by "tricky question". Have a look Here
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3606
    
  15
Sam Mercs wrote:Exactly what phrase do you want us to search with?


floating point equality


Joanne
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 605

Nan. Interesting to say the least!!!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39100
    
  23
So you got it. Please don't use thread titles like "tricky question... ?" Look at this FAQ.

Now: I challenge you: prove that such behaviour of NaN is logically correct.
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 605

Campbell Ritchie wrote:So you got it. Please don't use thread titles like "tricky question... ?" Look at this FAQ.

Now: I challenge you: prove that such behaviour of NaN is logically correct.


You mean besides your proof of 2=3?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39100
    
  23
Oh, you have found it? Yes, beside my proof of 2=3. You could try proving 3=5!
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 605

It was a great question though. Posting one too in the Puzzles Forum
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Tricky Question...?