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 ...
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
  • 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);
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
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
  • Mark post as helpful
  • send pies
  • Report post to moderator
Did you post this question before???
ah... yes.. you did! Just find the links
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,
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!