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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

about reference

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1246
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic