File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes StringBuffer and String returned from a method doubt 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 "StringBuffer and String returned from a method doubt" Watch "StringBuffer and String returned from a method doubt" New topic
Author

StringBuffer and String returned from a method doubt

Gitesh Ramchandani
Ranch Hand

Joined: Feb 28, 2007
Posts: 274
For the following code:
1. public class X {
2. public static void main (String[]args) {
3. string s = new string (�Hello�);
4. modify(s);
5. System.out.printIn(s);
6. }
7.
8. public static void modify (String s) {
9. s += �world!�;
10. }
11. }

The program runs and prints �Hello�.

But for the following code:

public class test {
public static void main (String[]args) {
StringBuffer s = new StringBuffer ("Hello");
modify(s);
System.out.println(s);
}
public static void modify (StringBuffer s) {
s.append("world!");
}

}

The program runs and prints �Hello world!�.

Why for StringBuffer it is getting updated and not for String??

pls help
regards,
gitesh
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959

StringBuffer is mutable whereas String is immutable.


SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
My Blog
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

I like to refer you this Article : StringBuffer versus String

hope it makes you more clear in it..


Saifuddin..
[Blog][Linkedin] How To Ask Questions On JavaRanch My OpenSource
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Note: Please quote your sources.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Gitesh Ramchandani
Ranch Hand

Joined: Feb 28, 2007
Posts: 274
source: my brainchild

Another bouncer:

class test1{
static StringBuffer sb = new StringBuffer("sb");
void ssb(StringBuffer sb) {
sb=sb.append("modified");
//return sb;
}



static String s = "string";
void ss(String s) {
s+="modified";
//return s;
}



public static void main(String args[]){
test t = new test();
t.ss(s);
System.out.println(t.s);
t.ssb(sb);
System.out.println(t.sb);
}
}

The above question gives output as:

string
sb

But according to the discussion above it should give
string
sbmodified

I'm in doubt again

pls help,
gitesh
Alexsandra Carvalho
Ranch Hand

Joined: Jul 13, 2007
Posts: 75
Hello Gitesh,
You are mistake. I have tested and the result is:

string
sbmodified
Gitesh Ramchandani
Ranch Hand

Joined: Feb 28, 2007
Posts: 274
Thanks Alexsandra, I made a mistake. It is printing correctly.

Dout Cleared, thanks Ranchers
 
Don't get me started about those stupid light bulbs.
 
subject: StringBuffer and String returned from a method doubt
 
Similar Threads
Queries
How to Compare StringBuffer object with String Object
about reference
don't understand
why not output is helloworld