Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can you please explain the output of this program?

 
shoeb sayyed
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Baiju Scariah
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
StringBuffer c=new StringBuffer("def");

and

"StringBuffer c" in "static void operate(StringBuffer b, StringBuffer c) " are not same.

When you call "c=b;", value of b is assigned to the temporary varibale, not to the c created in main().

So, if you print c in operate, result will be "abcdef", in main as it is not modified (only its copy is modified) it would print "def"..

Good to spend some time understanding differences between String & StringBuffer and concepts of pass by value & pass by reference..



 
shoeb sayyed
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Baiju,

It was really helpful for me
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Similar Threads