• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error in question at The JavaRanch Rules Round-up Game

 
Cecilia Burman
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Cecilia Burman
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 21390
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cecilia,

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

 
Cecilia Burman
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see that isn't your first post, but nobody has yet said this (sorry)
Cecilia Burman, welcome to JavaRanch
 
Cecilia Burman
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
/Cecilia
 
Mike Simmons
Ranch Hand
Posts: 3041
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic