• 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

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Integer i= new Integer("10");
if (i.toString() == i.toString())
System.out.println("Equal");
else
System.out.println("Not Equal");

it prints Not Equal,can anyone explain me why?
 
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
never compare object instances using the == operator. It's not going to work.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The == operator returns true if both variables point to the same object.

When comparing strings this may or may not be the case.
The JVM pools strings with the same value which sometimes causes == to return true.

To compare strings use the String.equals(String) method.
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#equals(java.lang.Object)
[ August 20, 2006: Message edited by: Ben Souther ]
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
FYI, the arsenal of methods for doing this allows you also to do MyString.equalsIgnoreCase(anotherString) which is similar to equals, except it ignores case, obviously

Also, (MyString.compareTo(anotherString) == 0) or
(MyString.compareToIgnoreCase(anotherString) == 0).

For non-ascii Strings, you should not use any of the above and instead use a Collator, which is found in the java.text package.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Brian Mozhdehi:

For non-ascii Strings, you should not use any of the above and instead use a Collator, which is found in the java.text package.



All Strings in Java are unicode strings. Collators are good for strings with Locale-specific characters. That has less todo with different technical encodings, but more with different sorting habits in different languages and cultures.
 
Ela Jha
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for the answers
 
reply
    Bookmark Topic Watch Topic
  • New Topic