• 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

==, why is it so ?

 
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
pls check this code
Byte b = new Byte("122");
String str = b.toString();
if(str == str)
System.out.println("str = " + str); // 1
if(b.toString() == b.toString())
System.out.println("==" );
else
System.out.println("not ==" ); // 2
// 1 and // 2 are getting printed..could anyone tell me the reason ??
thanks,
raj
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because an == comparison only works with Strings when they are in the String pool, not when they are created on the heap. the toString() method creates Strings on the heap. You need to do this:
b.toString().equals(b.toString())
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Raj,
As you're dealing with Strings you should use the .equals(String s) method that the String class overrides from Object. This evaluates the String value.
Using == on the Strings returned returns false because they are different String objects, even though their hashCode() may be the same.
Check out this code:
[CODE]
String s1 = "Hello";
String s2 = "Hello";
String s3 = new String("Hello");
System.out.println( s1.hashCode() );
System.out.println( s2.hashCode() );
System.out.println( s3.hashCode() );
System.out.println( s1==s2 ); // s1 and s2 refer to the same object in the String pool
System.out.println( s1==s3 ); // s1 and s3 refer to different objects, new String created
System.out.println( s1.equals(s2) ); // compares value
System.out.println( s1.equals(s3) ); // compares value
[/ CODE]

Cheers,
Kem
 
Rajeev Ravindran
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Thomas
thanks Kem
raj
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
raj,
Welcome to Javaranch
We'd like you to read the Javaranch Naming Policy and change your publicly displayed name (change it here) to comply with our unique rule. Thank you.
PS: Quote from the naming policy:


For your publicly displayed name, use a first name, a space, and a last name. Obviously fictitious names or improperly formatted names may be locked out.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic