Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question:String and StringBuffer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question:String and StringBuffer" Watch "question:String and StringBuffer" New topic
Author

question:String and StringBuffer

Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
hi,
what is the output?
1)public class RefTest {
public static void main(String [] args) {
String a = "first";
StringBuffer b = new StringBuffer("second"); manipulate(a,b);
System.out.println("a = " + a + ", b = " + b );
}
static void manipulate(String x, StringBuffer y) { String temp = x;
x = y.toString();
y.append(":xtra");
y = new StringBuffer(temp);
}
}
2)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);
}
}


nss
Roopa Bagur
Ranch Hand

Joined: Nov 03, 2000
Posts: 267
1. a = first, b = second:xtra
2. Hello how are you (is the output)
Originally posted by Neha Sawant:
hi,
what is the output?
1)public class RefTest {
public static void main(String [] args) {
String a = "first";
StringBuffer b = new StringBuffer("second"); manipulate(a,b);
System.out.println("a = " + a + ", b = " + b );
}
static void manipulate(String x, StringBuffer y) { String temp = x;
x = y.toString();
y.append(":xtra");
y = new StringBuffer(temp);
}
}
2)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);
}
}

Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
hi roopa,
thanx for prompt reply.

what significance does
y = new StringBuffer(temp); in question 1
and
s=s1 in question 2 have
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
this is to demonstrate that all arguments are passed by value. primitive value are copied and object references also !!! Since the reference is copied that means that you can't change the reference the caller provided as argument, but you can manipulate the referenced object though !
thus s=s1 won't change the reference s in the main method, the reference is only changed in the method manipulate but that's all.
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
 
jQuery in Action, 2nd edition
 
subject: question:String and StringBuffer
 
Similar Threads
Question from Javacaps mock
Queries
A String Buffer question,help!
I thought it should be "Hello" instead of "Hello how are you"
don't understand