File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Mock Exam Errata and the fly likes Simple Q on Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "Simple Q on Strings" Watch "Simple Q on Strings" New topic
Author

Simple Q on Strings

Vineet Sharma
Ranch Hand

Joined: Dec 30, 2000
Posts: 51
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

Joined: Jan 12, 2001
Posts: 11
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

Joined: Dec 30, 2000
Posts: 51
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

Joined: Jan 12, 2001
Posts: 11
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

Joined: Dec 30, 2000
Posts: 51
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

Joined: Jan 03, 2001
Posts: 63
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

Joined: Dec 30, 2000
Posts: 51
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

Joined: Dec 03, 2000
Posts: 89
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

Joined: Jun 30, 2000
Posts: 1070
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

Joined: Jan 21, 2001
Posts: 3
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!


k_arulvel
GanapathyRaman Natarajan
Greenhorn

Joined: Jan 22, 2001
Posts: 5
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 agree. Here's the link: http://aspose.com/file-tools
 
subject: Simple Q on Strings