Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

about reference  RSS feed

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Report post to moderator
7. Select the correct answer for the code fragment given below?
public class TestBuffer {
public void myBuf( StringBuffer s, StringBuffer s1) {
s.append(" how are you") ;
s = s1;
}
public static void main ( String args[] ) {
TestBuffer tb = new TestBuffer();
StringBuffer s = new StringBuffer("Hello");
StringBuffer s1 = new StringBuffer("doing");
tb.myBuf(s, s1);
System.out.print(s);
}
}
A) Prints Hello how are you
B) Prints Hello
C) Prints Hello how are you doing
D) Compile time error
Answer 7:
A) Prints Hello how are you
Explanation:
Assigning or interchanging the object references does not change the values, but if you change the values through object references , changes the values .
-----------
my question is that in line4 s1 is assigned to s , so I thought the output should be 'doing' , the appending operation is followed by the assigning operation , s is pointed to StringBuffer("doing") but i m wrong,why?
 
Ranch Hand
Posts: 1245
1
  • Mark post as helpful
  • send pies
  • Report post to moderator
Did you post this question before???
ah... yes.. you did! Just find the links http://www.javaranch.com/ubb/Forum24/HTML/013730.html
the out is not "doing" becuase changing the reference doesn't matter. After the method all references are gone. The old references back in main() stay the same. Pointing to the same Object.
Sorry if my post isn't clear. Mabye someone else will help if you still dn't understand.
Good luck on your exam.
[This message has been edited by FEI NG (edited December 19, 2001).]
 
Gold Digger
Posts: 7610
  • Mark post as helpful
  • send pies
  • Report post to moderator
wei liu,
DON'T POST THE SAME QUESTION TWICE, PLEASE !!!
Continue this discussion at the link provided by FEI NG above...
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
It is sorta covered in the JavaRanch Style Guide.
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!