This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Strings" Watch "Strings" New topic
Author

Strings

Tashi Rautela
Greenhorn

Joined: Jun 27, 2007
Posts: 22
if("String".replace('g','G') == "String".replace('g','G'))
System.out.println("Equal");
else
System.out.println("Not Equal");

--> The above code gives "Not equal" where as the below code gives "Equal"
if("String".replace('G','g') == "String".replace('G','g'))
System.out.println("Equal");
else
System.out.println("Not Equal");

If i give

if("String".replace('t','T') == "STring")

System.out.println("Equal");
else
System.out.println("Not Equal");


It gives Not equal

Not understanding this variation.Can someone please explain it..

Thanks,
Tashi
greg buela
Ranch Hand

Joined: Sep 04, 2007
Posts: 71
As you probably know, comparing Strings with == yields true only when both are the same String instance.
When you have duplicate String literals in your code, they are resolved to a single String instance. That is the case for the various "String" in that code, they are the same instance.
In the first piece of code, method replace() returns a new String instance that is the result of applying that replacement on the original instance. You have two calls to replace() on the same original instance, yielding 2 new String instances with the results. The results are "equal" but not the same instance, therefore == yields false.
In the second piece of code, replace() finds nothing to replace, and so it returns the original String instance, not a new one. Both results are then the same original instance, so == yields true.
In the last piece, replace() returns a new instance. The literal "STring" and the result are equal() but they're separate instances. Therefore == yields false.
If it still doesn't make sense to you, make sure to learn how Java works with Strings in general, literal Strings in particular, and the operator ==.


SCJP 1.5
Ankith suresh
Ranch Hand

Joined: Jun 07, 2007
Posts: 42
Hi Greg

if("String".replace('g','G') == "String".replace('g','G'))

in above code

1. first operand "String".replace('g','G') == --> This will create new Object ie "StrinG"
2. second operand "String".replace('g','G') --> This will also create object ie "StrinG"
My doubt is the object created in line 2 allready exist in the pool.In that case will it create a new object
greg buela
Ranch Hand

Joined: Sep 04, 2007
Posts: 71
Ankith,
Strings resolved at runtime and Strings explicitly created with new (even if the initial value is a literal that is in the pool) are not automatically interned (mapped to the pool). They are distinct instances, unless the intern() method is called.
Method replace() is probably using new behind the scenes!
Tashi Rautela
Greenhorn

Joined: Jun 27, 2007
Posts: 22
Thanks greg for the explaination...
this means replace function make the new instance always if changes are there & what about rest of the functions like substring,concat etc. Do they also behave in same manner.
Also intern() method is new thing for me...can please tell me what is it...

Thanks,
Tashi
greg buela
Ranch Hand

Joined: Sep 04, 2007
Posts: 71
I think it's pretty safe to say that substring and such methods create new instances too, you can make a simple test to compare the results (with ==) against a String literal that has the same contents.
The intern() method gives you a reference to the String pool instance that is equal() to your String. I don't remember what happens if it doesn't exist, I have never actually used it. But look it up, you will quickly find more valuable information than anything I can say here
Thiru Mu
Greenhorn

Joined: Aug 30, 2007
Posts: 23
Greg,

if("String".replace('g','G') == "String".replace('g','G'))
System.out.println("Equal");
else
System.out.println("Not Equal");

if("String".substring(2,4) == "String".substring(2,4))
System.out.println("Equal");
else
System.out.println("Not Equal");


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

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


I ahve tested all the above, all prints "Not Equal" so it means, the methods substring,replace, trim and concat are creating new instances behind the scenes!

Is this seem good?
greg buela
Ranch Hand

Joined: Sep 04, 2007
Posts: 71
Yeah, if they fail the == test, they're definitely separate instances.
 
 
subject: Strings
 
Similar Threads
String Question
String Methods
Different behaviour with string objects
About String
Java Strings