• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

comparison involving primitive and wrapper

 
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The Above from the K&B exam. What I dont understand is when we do i==j where on of them is a primitive(j), it turns out to be true despite the fact that these values are both above 127. But in the go() where both comparisons involve Wrappers, the test fails. Why?
is Integer i being demoted to a primitive in the first comparison?
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Box2 {
public static void main(String[] args) {
Integer i = 500;
Int j = 500;
if(i == j) System.out.print("a");
if(i.equals(j)) System.out.print("b");
new Box2().go(j, i);
}
void go(Integer x, Integer y) {
if(x == y) System.out.print("c");
if(x.equals(y)) System.out.print("d");
}
}

I Think Your Declaration of j is illegal,since the i in 'Int' is capital,it has to be either int or Integer
 
Amit Batra
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry bout that, but still I my doubt is uncleared. Can someone answer me this?
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Amitabha, you are right.

In first case, since j is int so the comparison is between primtives. Integer object gets unboxed to int so equality operator returns true. Second case is quite clear as both are references and range is outside -128 to +127

Naseem
 
P.Praveen Jesudhas
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what i meant was:

integer has to be either declared as "int x" for primitives or as "Integer x" for putting inside wrapper classes."Int j"is an illegal declaration
 
Amit Batra
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Naseem
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic