Win a copy of Fixing your Scrum this week in the Agile forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Wrapper types comparison doubt

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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??
 
author
Posts: 23905
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Kedar Pethe
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 miss the old days when I would think up a sinister scheme for world domination and you would show a little emotional support. So just look at this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic