This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General 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