aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question from Javacaps mock Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question from Javacaps mock" Watch "Question from Javacaps mock" New topic
Author

Question from Javacaps mock

geeta rai
Ranch Hand

Joined: Sep 18, 2003
Posts: 48
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);
}
}
The answer is "Hello how are you". My question is that since the references of s and s1 have been passed to the method myBuff, why wouldn't s refer to where s1 is refering after the statement s=s1?
Thanks.
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
No, we cannot change the value contained in a StringBuffer by assigned with another variable using "=". Your StringBufer s will be referring to the variable s1. U will see in the following modified code... I reversed the value object s1, which is referred by s. The result is "gniod". I mean that s is referring to s1, not s'value is changed... Hope it is clear...

public class TestBuffer {
public void myBuf( StringBuffer s, StringBuffer s1) {
s.append(" how are you") ;
s = s1;
s.reverse();
}
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(s1);
}
}


Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question from Javacaps mock