Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple Q on Strings

 
Vineet Sharma
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
public class Test{
public static void main(String args[])
{
String s1 = new String ("amit");
String s3 = new String ("arit");
String s2 = s1.replace('m','r');
System.out.println(s2==s3);
}
}
The answer to this code is false. Can some body explain?
I think it should be true and my reasoning is that the line "s1.replace('m','r')" will return a new string containing "arit". Now since the string "arit" already exists in the string pool (as created by s3) s2 will also start pointing to the same memory location. Therefore the variables s2 and s3 are now seen pointing to the same memory location and hence s2==s3 should be true!
Sorry for the long explanation. Once again thanks to all for the help...
Regards
Vineet
 
mahesh jhamtani
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this
public class Test{
public static void main(String args[])
{
String s1 = new String ("amit");
String s2 = new String ("arit");
String s3 = s1.replace('m','r');
System.out.println(s2.equals(s3));
}
}
 
Vineet Sharma
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi to all,
I am sorry I typed the wrong code !
The right one is:
public class Test{
public static void main(String args[])
{
String s1 = new String ("amit");
String s3 = "arit" ;
String s2 = s1.replace('m','r');
System.out.println(s2==s3);
}
}
The answer to this code is false. Can some body explain?
I think it should be true and my reasoning is that the line "s1.replace('m','r')" will return a new string containing "arit". Now since the string "arit" already exists in the string pool (as created by s3) s2 will also start pointing to the same memory location. Therefore the variables s2 and s3 are now seen pointing to the same memory location and hence s2==s3 should be true!
Sorry for the long explanation. Once again thanks to all for the help...
Regards
Vineet

 
mahesh jhamtani
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you equate object this way...
String is object in java not primitive type..
So if we use == operator to equate instance of String object
we will get false only.
Originally posted by Vineet Sharma:
Hi to all,
I am sorry I typed the wrong code !
The right one is:
public class Test{
public static void main(String args[])
{
String s1 = new String ("amit");
String s3 = "arit" ;
String s2 = s1.replace('m','r');
System.out.println(s2==s3);
}
}
The answer to this code is false. Can some body explain?
I think it should be true and my reasoning is that the line "s1.replace('m','r')" will return a new string containing "arit". Now since the string "arit" already exists in the string pool (as created by s3) s2 will also start pointing to the same memory location. Therefore the variables s2 and s3 are now seen pointing to the same memory location and hence s2==s3 should be true!
Sorry for the long explanation. Once again thanks to all for the help...
Regards
Vineet

 
Vineet Sharma
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mahesh,
... if so then why do we get the following as true:

String str1 = "abc";
String str2 = "abc";
if(str1==str2)
 
Pat Barrett
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Vineet,
When you initialize Strings in that way you are actually only creating references, not objects. Comparing the references would therefore be true. If the Strings were initialized as follows:
String s1 = new String("abc");
String s2 = new String("abc");
the comparison would be False, because the objects were being comapred.
Hope this helps.
Pat B.
 
Vineet Sharma
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you explain this:

public class Test{
public static void main(String args[])
{
String s1 = new String ("amit");
String s3 = "arit" ;
String s2 = s1.replace('m','r');
System.out.println(s2==s3);
}
}
The answer to this code is false. Can some body explain?
I think it should be true and my reasoning is that the line "s1.replace('m','r')" will return a new string containing "arit". Now since the string "arit" already exists in the string pool (as created by s3) s2 will also start pointing to the same memory location. Therefore the variables s2 and s3 are now seen pointing to the same memory location and hence s2==s3 should be true!
Vineet
[This message has been edited by Vineet Sharma (edited January 14, 2001).]
 
Rob Acraman
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vineet,
Comparing new strings to those that are already in the string-pool is a compile-time-only feature, not run-time.
This is because cross-checking constants has a negligible one-off cost on the time it takes to compile a program, and (more to the point) there is no run-time impact.
On the other hand, if this check were to be performed at run-time, performance would suffer terribly as every string assignment throughout the system would have to be compared against every other value.
So, in your example, the 'replace' takes the quick route of simply creating a new String object, which will therefore have a different address from s2. Hence, s2==s3 is false.
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vineet,
The answer is actually pretty easy to remember if you know that this will be false:
String s1 = new String("java");
String s2 = new String("java");
(s1 == s2) //this will be false
This is false because anytime you create strings with the new keyword, you get a new object regardless of the fact that the string value is the same. Java provides optimization if you don't use the keyword new by looking to see if the string is already in the pool and using that instance.
So, if you agree with me that with new you always get a new String object, then remember this: Anytime you call a String method and the method will change the String value, the method will return a new String. So if the replace function has something to replace, it is returning a new String and hence the rules above apply and they won't be equal. Try the function again by replacing q with a which has no effect since q is not in the original String and this won't return a new String so it will still be equal to the old string.
Hope that makes sense,
Bill
 
k_arulvel
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Just to add to that !
but if you user toLowerCase and toUpperCase functions, if the String you are dealing with is already has those uppercase/lowercase already, then the same reference is returned.
new String is not built and returned.
Hope this will be helpful!
 
GanapathyRaman Natarajan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
String s2 = s1.replace('m','r');
The line above s1.replace('m','r') return's a String object...
it is same as creating String s2 = new String("amit");
When you give (s2==s3) the answer is false, because they r referring to different memory locations..
Hope this answer help
Thanks
Ganapathy

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic