aspose file tools*
The moose likes Beginning Java and the fly likes string problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "string problem" Watch "string problem" New topic
Author

string problem

Rippon Jalali
Greenhorn

Joined: Aug 25, 2006
Posts: 23
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");
Sundeep Gupta
Greenhorn

Joined: Feb 28, 2006
Posts: 13
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.


SCJP 5.0
Rippon Jalali
Greenhorn

Joined: Aug 25, 2006
Posts: 23
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");
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14338
    
  22

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

See java Doc for the mehtod String.intern()


Saifuddin..
[Blog][Linkedin] How To Ask Questions On JavaRanch My OpenSource
H. J. Yoshi
Greenhorn

Joined: Jul 21, 2006
Posts: 29
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"..
Anton Uwe
Ranch Hand

Joined: Jan 10, 2007
Posts: 122
toUpperCase() will return the reference of the String it was called on when it sees that no changes are needed.
Rippon Jalali
Greenhorn

Joined: Aug 25, 2006
Posts: 23
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

Joined: Dec 06, 2005
Posts: 1321

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

Joined: Jul 21, 2006
Posts: 29
Yes, got it! Thanks a lot, everybody.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: string problem