Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java pass by reference

 
joao ferreira
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

//result s=aaa sb=bbbaaabbb
i=5 j=7
null

Why the StringBuilder sb change the value?
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See the code in this post.
 
Chan Ag
Rancher
Posts: 1089
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why the StringBuilder sb change the value?


Because sb is pointing to a mutable object. I hope you know why the variable s prints the same value.

[ Edit - I'd suggest to read the entire thread E Armitage is referring to. ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic