Forums Register Login

Simple Q on Strings

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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));
}
}
+Pie Number of slices to send: Send
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

+Pie Number of slices to send: Send
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


+Pie Number of slices to send: Send
Hi Mahesh,
... if so then why do we get the following as true:

String str1 = "abc";
String str2 = "abc";
if(str1==str2)
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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).]
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
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

I hired a bunch of ninjas. The fridge is empty, but I can't find them to tell them the mission.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1291 times.
Similar Threads
doubt about Strings from Whizlabs
about string
question on Strings
Strings are immunable - What does this mean?
one more on ==
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 08:59:23.