• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

== operator for Strings

 
vidhya suvarna
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Henry Wong
author
Marshal
Pie
Posts: 21515
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
vidhya suvarna
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21515
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
replace method returns a String instance

String instances each have their own address space

Hence == returns false
 
himanshu kesarwani
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..!!
 
Mahendar Reddy
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Muhammad Ahsan Jamshaid
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(str==ttt) must return true

(str == ttt) is returning false..


@jamshaid...
 
Mahendar Reddy
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic