• 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

string problem

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the below given code gives "Not Equal". But as trim() returns a string whose value is string used to invoke the method but with any leading or trailing blank spaces removed. So isn't the condition result in true and output be "Equal"?

if(" String ".trim() == "String")

System.out.println("Equal");

else

System.out.println("Not Equal");
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you try to compare two strings using ==, Java compares thier object references and result will be false, even if lexically the strings are equal. This is because Strings in Java are immutable and hence separate object is created for each string.
To compare two strings lexically, use equals() method.
 
Rippon Jalali
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but when we are comparing string literals there is no need of equals method.As when we run the code below.it gives output "Equal".
if("String".toString() == "String")

System.out.println("Equal");

else

System.out.println("Not Equal");
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But " String ".trim() is not a String literal. You call a method on a String literal and it returns a new String object, of which the value is "String" (without the leading and trailing white space).

As to why == normally does work with string literals, that's a more complicated story. It is because of an optimization that the Java compiler has for strings, the string pool.

Never rely on == for comparing strings. Always use equals(), also for string literals.
 
Ranch Hand
Posts: 1325
Android Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See java Doc for the mehtod String.intern()
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I agree with this.. but then I tried out this:


This prints "Equal".Again, .toUpperCase() returns a String object then how is it returning true..

The following though prints "Unequal"..
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
toUpperCase() will return the reference of the String it was called on when it sees that no changes are needed.
 
Rippon Jalali
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As the calls to "STRING ".toUpperCase() == "STRING" does not change anything, the reference to the orignal unchanged string in the pool is returned.
 
Muhammad Saifuddin
Ranch Hand
Posts: 1325
Android Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Himadri Joshi:



This prints "Equal".Again, .toUpperCase() returns a String object then how is it returning true..



toUpperCase() methods Converts all of the characters in this String to upper case.

in your case "STRING" and comparative STRING is already defined in uppercase try this toLowerCase() then you would fine you answer..
[ January 16, 2007: Message edited by: Saif uddin ]
 
H. J. Yoshi
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, got it! Thanks a lot, everybody.
reply
    Bookmark Topic Watch Topic
  • New Topic