aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String question" Watch "String question" New topic
Author

String question

Chitra AP
Ranch Hand

Joined: May 25, 2005
Posts: 42
class TestString {
public static void main (String[] args) {
if (" String ".trim() == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");
}
}

Please let me know the answer and explanation. Thx.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Sure. What's the question?


[Jess in Action][AskingGoodQuestions]
Jaswanth Yalavarthi
Greenhorn

Joined: May 29, 2005
Posts: 1
Answer is Not Equal.

Explanation:

== does reference equality.
even though " String ".trim() yeilds you the literal value "String" ; it doesn't reference the same object as "String".
This is because " String ".trim() give the result at run time and for the strings to be interned it has to be the same value at compile time.
Hope this helps
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi Jaswanth,

Welcome to JavaRanch!

That's a pretty good explanation, but it's unfortunately incorrect; the code will print "Equals". This happens because trim() is smart enough to return its argument if it's unchanged, rather than a copy of the argument; this is documented in the Javadoc for the trim() method.
Chitra AP
Ranch Hand

Joined: May 25, 2005
Posts: 42
Jaswanth,

your answer is correct and I understand your explanation too. So, the same explanation shoule be applicable for this code also, correct?

if("String".toString() == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");

But it prints equal. why? Thanks.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Interesting. It looks like EFH gave an incorrect answer because he didn't notice the spaces in " String " in the original code. (Chitra, please use [code] tags in the future to make your code easier to read.) However now that Chitra has asked, effectively, what happens if we remove the spaces to get "String" - now EFH's answer is correct for the new question. So congratulations, EFH, for psychically sensing a question from the future, and answering it correctly.


"I'm not back." - Bill Harding, Twister
Vipin Das
Ranch Hand

Joined: Jul 05, 2004
Posts: 47
Hi Chitra,
If u put " string " with spaces at both ends or a space at either end, the method will create a new string. But if u put like this "string" with no spaces in the end, it will return the same string, which is already in the string pool. That is why it prints "equals". If u have any doubt in string methods, it is better to have a look at the source code.
SomeswaraRao Vudattula
Greenhorn

Joined: Mar 28, 2005
Posts: 12
Hi Chitra,

already we know
"==" copmares the heap memorylocations of Strings and
.equals() compares the contents of Strings..

in this case, " String " == "String"
first String will create once.. and second String will create once again..because both r diff..Strings...so there memory locations r diff
so..

if(" String ".trim()== ("String"))
System.out.println("Equal");
else
System.out.println("Not Equal");

OutPut: Not Equal

but if u modify the code like this..

if(" String ".trim().equals("String"))
System.out.println("Equal");
else
System.out.println("Not Equal");

OutPut: Equal
Chitra AP
Ranch Hand

Joined: May 25, 2005
Posts: 42
Thank you. It clears my doubt.
ashok_g
Greenhorn

Joined: May 30, 2005
Posts: 1
Hi Friends. I was just following this String Class duscussion here. Can someone explain me why the following code produces an output "Equal". I think it should be "Not Equal" cause both "String" declarations create two different objects. while the tostring() on the first "String" object returns back the same "String" object still its different then the actual second "String" object.

[ June 01, 2005: Message edited by: Ashok Golani ]
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
Why do you think the two "String" declarations create two different objects? That is just the opposite of what the spec says. Both "String"'s are compile time constants and will be placed once into the String pool, so they will be the same object.
Raja Sagar Panamgipalli
Ranch Hand

Joined: Aug 13, 2003
Posts: 109
The API says ...
The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).


so after getting trimed...the new "String" object is actually the one already inside the String pool.so they both contain the same bitpattern refering to the same address on the heap.

I think Steven also made the same point.

Jim and EFH can correct me if i wrong...

Regards

Sagar


SCJP 1.4<br />SCBCD 1.3
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
guys...as written in K&B ...

operation on string creates new string only when there is some alteration in the string object that invokes the method else
it retun the same reference...

like in following it return the same reference...

String a="abc"
a.toString();=="abc" // true
a.toLowerCase();=="abc" // true
a.trim(); =="abc" // true

in all the above cases ...no alteration is made in string refernce a even different method are invoked..which they didn't cause any alteration so ..same string reference is maintain

in following code ...it will new string will be created

String a="ABC"
a.toLowerCase();=="abc" // false
"abc ".trim(); =="abc" // false


hope it clears..

do acknolwedge this post


Thanks and Regards, Amit Taneja
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String question