This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes string doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "string doubt" Watch "string doubt" New topic
Author

string doubt

amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
hi ranchers...

in one of the DAN question...as follow


class MWC114 {
public static void main(String[] s) {
String s1 = new String("ABCDEFG"), s2 = new String("EFGHIJ");
String s3 = s1.substring(4,7), s4 = s2.substring(0,3);
System.out.println((s3 == s4) + "," + (s3 + s4).equals(s4 + s3));
}}

What is the result of attempting to compile and run the program?

a. Prints: false,false
b. Prints: false,true
c. Prints: true,false
d. Prints: true,true
e. Compile-time error
f. Run-time error
g. None of the above


the answer is "b"
with following explainationg...

The reference variable s1 is initialized with a reference to an instance of type String containing the value "ABCDEFG". The reference variable s2 is initialized with a reference to an instance of type String containing the value "EFGHIJ". The expression s3 = s1.substring(4,7) initializes the reference variable s3 with a reference to a unique instance of type String containing the value "EFG". The expression s4 = s2.substring(0,3) initializes the reference variable s4 with a reference to a unique instance of type String containing the value "EFG". The expression s3 == s4 compares two unique reference values and produces the value false even though s3 and s4 reference two String instances that contain the same value, "EFG". The expression s3 + s4 produces a unique instance of type String containing the value "EFGEFG". Similarly, the expression s4 + s3 produces a unique instance of type String containing the value "EFGEFG". The expression (s3 + s4).equals(s4 + s3) compares the contents of two unique instances of type String that contain the value "EFGEFG". The result of the comparison is true.


..but i have a doubt..that in question why s3 and s4 don't refer to the same string object in memeroy pool.. ? are they refering differnt...

but what happen when the string object is in the memeroy pool and s3 and s4 will internally has refernce to same string object ...as written in k&B and as java spect...... even then they are not true acording to == test ??

or s3==s4 will only be true when either of them is created by other..?

give ur clarifications.....


Thanks and Regards, Amit Taneja
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
If you read the API for String.substring you will see that:
Returns a new string that is a substring of this string



So s3 and s4 refer to different strings. That's why s3 == s4 results in false.
The contents of the strings referred to by s3 and s4 are the same character sequence "EFG", so the string returned by s3 + s4 ("EFGEFG") has the same contents as that returned by s4 + s3. Notice also that s3 + s4 is a different object from the object representing s4 + s3.
[ May 12, 2005: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
ya... thanx...just wanted to clear...weather all different string will not be created when assingment from one reference variable to other reference variable is done ....

else created...



thanx
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: string doubt
 
Similar Threads
Doubt in String...Plz Help Me
General problems..
Strings
Doubt on Strings
string assignment