Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

StringBuffer and String returned from a method doubt

 
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
StringBuffer is mutable whereas String is immutable.
 
Ranch Hand
Posts: 1325
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I like to refer you this Article : StringBuffer versus String

hope it makes you more clear in it..
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note: Please quote your sources.
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Gitesh,
You are mistake. I have tested and the result is:

string
sbmodified
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Alexsandra, I made a mistake. It is printing correctly.

Dout Cleared, thanks Ranchers
 
You ought to ventilate your mind and let the cobwebs out of it. Use this cup to catch the tiny ads:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic