• Post Reply 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
  • Tim Cooke
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Junilu Lacar
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst
  • Peter Rooke
  • Mikalai Zaikin
Bartenders:
  • Himai Minh

question:String and StringBuffer

 
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
}

 
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi roopa,
thanx for prompt reply.

what significance does
y = new StringBuffer(temp); in question 1
and
s=s1 in question 2 have
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Did Steve tell you that? Fuh - Steve. Just look at this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic