*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes == operator for Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "== operator for Strings" Watch "== operator for Strings" New topic
Author

== operator for Strings

vidhya suvarna
Ranch Hand

Joined: Aug 28, 2008
Posts: 148
Source: http://examsguide.com/scjp/freequestions8.html


class A{
public static void main(String[] args) {
String s="String".replace('t','T');
String s1="String".replace('t','T');
if(s == s1)
System.out.println("Equal");
else
System.out.println("Not Equal");
System.out.println(s+" "+s1);
}}

The output of the above code is "Not Equal".

However "String".replace('t','T') returns "STring". so s1 and s will both have value "STring" which according to me will be stored in the pool of string. Hence == should in that case return TRUE.

Can anyone tell me that the output is "Not Equal" because replace() create a new String object?


SCJP 1.4 - 88%<br />SCWCD 1.5 - Preparing
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18511
    
  40

However "String".replace('t','T') returns "STring". so s1 and s will both have value "STring" which according to me will be stored in the pool of string....


Why do you believe that the results from the replace() method (will return) is a string that is in the string pool?

Henry
[ September 20, 2008: Message edited by: Henry Wong ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
vidhya suvarna
Ranch Hand

Joined: Aug 28, 2008
Posts: 148
i believe that replace() will return "STring" a string literal which will be added to the pool if it already doesnot exist.
Kindly correct me if i am wrong.

Thanks
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18511
    
  40

Originally posted by vidhya suvarna:
i believe that replace() will return "STring" a string literal which will be added to the pool if it already doesnot exist.
Kindly correct me if i am wrong.

Thanks


Here is reference to the JavaDoc...

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html

There is *no* mention of the replace() method ever returning an interned string.

Henry
Leonid Shchervinsky
Ranch Hand

Joined: Sep 07, 2006
Posts: 32
replace method returns a String instance

String instances each have their own address space

Hence == returns false


<a href="http://www.shchervinsky.com/java/" target="_blank" rel="nofollow">http://www.shchervinsky.com/java/</a>
himanshu kesarwani
Ranch Hand

Joined: Aug 27, 2008
Posts: 84
hi....every one!!
the thing is that whether you use "replace"....or not doesnt matter because the == works on same memeory check.....i.e it can b used for a single objects variables.
even this is wrong;
String str="hi";
String ttt="hi";
if(str==ttt)
//if wont come here
.....
reason is different objects thats it..!!
every where they will be treated as different objects!!
use..
object.equalto(object);
it will return true..!!


SCJP 5
Mahendar Reddy
Ranch Hand

Joined: Sep 27, 2005
Posts: 73
String str="hi";
String ttt="hi";
if(str==ttt)
//if wont come here
.....
reason is different objects thats it..!!


Hey Himanshu, are you sure about this? Because (str==ttt) must return true. It is returning true. How can you say both are different objects without testing?


SCJP- 98%<br />SCWCD-92%
Muhammad Ahsan Jamshaid
Ranch Hand

Joined: Jun 01, 2006
Posts: 59
(str==ttt) must return true

(str == ttt) is returning false..


@jamshaid...


--Ahsan Jamshaid... SCJP 5(80%)
Mahendar Reddy
Ranch Hand

Joined: Sep 27, 2005
Posts: 73
Hi,
String s1="hi";
String s2="hi";
System.out.println("Test1: "+(s1==s2));


The above code snippet is returning 'Test1: true'.(Java version 1.6.0_05)
Muhammad Ahsan Jamshaid
Ranch Hand

Joined: Jun 01, 2006
Posts: 59
you are right it returns true i was do it as System.out.println("Test1: "+s1==s2);
which return false.. note that "Test1: "+s1 are concatenated first and then compared thats why it was return false...

@JAMSHAID..
 
Consider Paul's rocket mass heater.
 
subject: == operator for Strings
 
Similar Threads
String Equality
confused with String Equality
About String
Clrify results from SCJP mock exam site
String compare