It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Wrapper types comparison doubt 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 "Wrapper types comparison doubt" Watch "Wrapper types comparison doubt" New topic
Author

Wrapper types comparison doubt

Kedar Pethe
Ranch Hand

Joined: Jul 17, 2012
Posts: 39
Chapter 6, Self test Q3.

Correct answers - B,C,D

How come answer is also D option??
I know that two Integer wrapper objects can be compared.

Using simple relational operator,
How can a short object be compared with an Integer object?
How does Java compare the two different wrapper's values??
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16687
    
  19

Kedar Pethe wrote:Chapter 6, Self test Q3.

Correct answers - B,C,D

How come answer is also D option??
I know that two Integer wrapper objects can be compared.

Using simple relational operator,

How can a short object be compared with an Integer object?
How does Java compare the two different wrapper's values??


The answer is .... it depends.

If you are using a "==" or "!=" as the operator, it will do an instance comparison, meaning whether the two instances are the same instance.... and of course, this will fail to compile as Short and Integer are not sub/super classes of each other.

Otherwise, such as what you did in the example, it will do a value comparison, meaning it will unbox both sides first.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Kedar Pethe
Ranch Hand

Joined: Jul 17, 2012
Posts: 39
Henry Wong wrote:
If you are using a "==" or "!=" as the operator, it will do an instance comparison, meaning whether the two instances are the same instance.... and of course, this will fail to compile as Short and Integer are not sub/super classes of each other.
Otherwise, such as what you did in the example, it will do a value comparison, meaning it will unbox both sides first.
Henry


Ok.. but is it that while using >,<,<=,>= whatever be the two wrapper types(Byte,Short,Integer,Long,Float,Double) comparison will always take place on their values??
Kedar Pethe
Ranch Hand

Joined: Jul 17, 2012
Posts: 39
Henry Wong wrote:
If you are using a "==" or "!=" as the operator, it will do an instance comparison, meaning whether the two instances are the same instance.... and of course, this will fail to compile as Short and Integer are not sub/super classes of each other.

Otherwise, such as what you did in the example, it will do a value comparison, meaning it will unbox both sides first.

Henry

I tried, whatever be the two operands for relational operators, comparison always takes place on their values and gives correct output, without any error!!
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Wrapper types comparison doubt
 
Similar Threads
Strings & Boxing/Unboxing Question
String doubts from K&b Self Test
SCJP 1.5, Chapter 6 question 4
Questios having in correct answers
Queries--