• 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 question

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure. What's the question?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. It clears my doubt.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic