• 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

UNable to find out difference

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers

i unable to distinguish why all the out put is "hello".
This is example from kathy siera book

Integer x=10;
Integer y=10;
if(x==y)
System.out.println("hello");

if(x!=y)
System.out.println("hello");

if(x.equals(y))
System.out.println("hello");
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Page 236 of K&B book have the explanation for this.
Here is part of it for your reference:

...
two instances of the
following wrapper objects will always be == when their primitive values are the same:
* Boolean
* Byte
* Character from \u0000 to \u007f (7f is 127 in decimal)
* Short and Integer from -128 to 127
...

 
srinibash udayasingh
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Thanks for the reply.

But my question if Integer wraper class reference hold same value then then == returns true.Then why != also gives true.
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi sri,
Are you sure that == returns true and != returns true.
I tried the following

Only hello is displayed. Run the program once and check.
[ March 12, 2008: Message edited by: kesava narayana ]
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Srinibash, it might help if you compile it with println statements like these:
 
There were millions of the little blood suckers. But thanks to this tiny ad, I wasn't bitten once.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic