It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String Equality Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String Equality" Watch "String Equality" New topic
Author

String Equality

Anil Pradhan
Greenhorn

Joined: Jan 03, 2008
Posts: 15
Can anyone please explain me the answer for this question.

if("String".replace('t','t') == "String"){
System.out.println("equal");
}else{
System.out.println("Not Equal");
}

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

Help is greatly appreciated!!
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
Good question.
public String replace(char oldChar,
char newChar);

a) - If the character oldChar does not occur in the character sequence represented by this String object, then a reference to this String object is returned.
b) - If oldChar and newChar are identical then a reference to this String object is returned.

c)Otherwise, a new String object is created that represents a character sequence identical to the character sequence represented by this String object, except that every occurrence of oldChar is replaced by an occurrence of newChar

In first case you invoke "String".replace('t', 't'), Here since oldChar and newChar in call to replace are identical a reference to exisinting "String" object is returned. Further since "String" is a literal that is used in your code , and as you know Strings are immutable and hence JVM the following will happen.
a. JVM will create a new String object in String pool for the string literal used in your code.
b. Due to above explanation , replace() will return the this string object and since this string object ["String"] is already in the String pool a reference to it is returned and hence the result is equal in first case.


Now coming to second case since oldChar and newChar are different in both the calls to replace a new string object is created in both the cases even though the final string is same. The JAVADOC API says that a new string object will be created.

Hope this clears good one.


SCJP, SCWCD, SCBCD
Anil Pradhan
Greenhorn

Joined: Jan 03, 2008
Posts: 15
Thanks a TON.
Excellent explanation.
pradeep singh
Ranch Hand

Joined: Oct 23, 2007
Posts: 339
Hi
Thanks Mr.Deepal Jain for giving a good explanation.

I want to know that from where you got all this type of deeply knowledge .Mostly books do not cover these in so much detail even K&B .
I am planning to give exam on 19 january ,but i do not have so deep knowledge like you.Please tell me from where should i read them or it is must required so deep knowledge to give test.
I am doing practice on whizlab but i never found a question like this as above.
Please guide me .

Waiting for your reply
[ January 08, 2008: Message edited by: pradeep singh ]

SCJP 5.0(75%), SCWCD 5.0(88%)
Raghu Doppalapudi
Greenhorn

Joined: Jun 26, 2007
Posts: 6
very good question anil & thanks deepak for good explanation


Doppalapudi
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
Hey,
My name is Deepak and not Deepal
I don't have any deep knowledge in Java and my aim is gaining more and more knowledge by participating in java forums.
When i saw this question, the first thing i did was run the code and second thing i did was read the Java API doc for the method and after that i understood the concept and explained .

For exam i would say you will need 3.5 hrs of complete concentration and offcourse some knowledge of Java thats the way i took my exam on Dooms day of 2007 Just keep reading and keep practicing.
All the best for your exam.
[ January 09, 2008: Message edited by: Deepak Jain ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String Equality
 
Similar Threads
String Question
String Methods
Different behaviour with string objects
About String
Strings