This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Ranch Office and the fly likes Error in question at The JavaRanch Rules Round-up Game Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » This Site » Ranch Office
Bookmark "Error in question at The JavaRanch Rules Round-up Game" Watch "Error in question at The JavaRanch Rules Round-up Game" New topic
Author

Error in question at The JavaRanch Rules Round-up Game

Cecilia Burman
Greenhorn

Joined: Jan 25, 2010
Posts: 6
Hi

I just rounded up some cattle at The JavaRanch Rules Round-up Game. I noticed the following question:
(#16) Integer a=new Integer(5); Integer b = new Integer(5); What is the result of running if (a==b)
The real answr is true, not false
The Integer (maybe most wrapper classes) is an exception to the rule stated in the explanation. It does return true for low numbers as the following code-snippet shows:



Kind Regards, Cecilia


Stop climate change at any cost. An inhabitable world is worth the price.
http://www.newscientist.com/data/images/archive/2697/26971701.jpg
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10067
    
163

I haven't checked the question in rules roundup, but the example you posted here:

Integer i=0;
Integer ii=0;


isn't the same as one in the game:

Integer a=new Integer(5);
Integer b = new Integer(5);


See this discussion, about what I mean.


[My Blog] [JavaRanch Journal]
Cecilia Burman
Greenhorn

Joined: Jan 25, 2010
Posts: 6
The discussion you refer to is exactly what I mean. Since the value (5) is lower than 127, the real answer is true, not false
Cecilia Burman
Greenhorn

Joined: Jan 25, 2010
Posts: 6
I also found this question:
(#98) All exceptions ingerit from:
With the explanation:
"The Exception hierarchy begins at java.lang.Throwable"
IMHO all exceptions inherit from both Throwable and Exception. Isn't that so?

:-)
/Cecilia
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10067
    
163

Cecilia Burman wrote:The discussion you refer to is exactly what I mean. Since the value (5) is lower than 127, the real answer is true, not false


It only applies if you do:



and not when you do:



From your first post, I believe the question in rules roundup is doing the latter?
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20536
    ∞

Cecilia,

Perhaps you could write a little program for us that proves your point. Be sure to run it and show us your output.


permaculture Wood Burning Stoves 2.0 - 4-DVD set
Cecilia Burman
Greenhorn

Joined: Jan 25, 2010
Posts: 6
My mistake I did not realize the difference between "=0" and "=new Integer(0)"

Thank you for explaining. I will be careful to copy the exact code next time I test a question.

/Cecilia
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10067
    
163

Cecilia Burman wrote:My mistake I did not realize the difference between "=0" and "=new Integer(0)"



No problem, after all you learnt something new
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38427
    
  23
I see that isn't your first post, but nobody has yet said this (sorry)
Cecilia Burman, welcome to JavaRanch
Cecilia Burman
Greenhorn

Joined: Jan 25, 2010
Posts: 6
Thanks
/Cecilia
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3011
    
  10
Cecilia Burman wrote:I also found this question:
(#98) All exceptions ingerit from:
With the explanation:
"The Exception hierarchy begins at java.lang.Throwable"
IMHO all exceptions inherit from both Throwable and Exception. Isn't that so?

:-)
/Cecilia

Not really. When people talk about "exceptions" or "exception handling", note the lower case, they often mean any Throwable. Like wise they often say "error" or "error handling" when they really mean any Throwable. The problem is that "throwable handling" isn't a term you could use outside the Java community, and it would be tedious to say "error and exception handling" instead. So informally, people use these terms more loosely. But if you capitalize the terms, it's clear you're talking about Error or Exception as specific Java classes, and the ambiguity goes away.
 
Don't get me started about those stupid light bulbs.
 
subject: Error in question at The JavaRanch Rules Round-up Game