Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

string doubt

 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.....
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic