I'm guessing you're making the mistake of thinking that the line y = x is going to affect the variable b? It doesn't. Java is pass-by-value, and it's a copy of the b variable that is passed into the method. It's still referencing the same object - that can be mutated.
I've added a bunch of comments into your code to help explain what's going on. Matthew got in there while I was typing this out, and he's right, the source of the misunderstanding is down to the fact that to the 'operate' method you're passing a reference to an Object, not the Object itself.
Tim Cooke wrote:I've added a bunch of comments into your code to help explain what's going on. Matthew got in there while I was typing this out, and he's right, the source of the misunderstanding is down to the fact that to the 'operate' method you're passing a reference to an Object, not the Object itself.
Thank you for your explaination.
Still have some doubt.
1. You have written // Remember Pass by Reference means that y and a refer to the same StringBuffer...how y and a refer to same object?
2. I am confused with last assignment. y=x; If you say, y has scope only inside this method, then why append function changes y's (b's) value in original function.
Like for following code, I am clear about the pass by value concept. And Result is 10,20. But for this StringBuffer example, I am confused.
When you pass a into the operate method, you are passing the reference and assigning it to a local variables called x and y. So you have this:
|a| ----> |StringBuffer|
So when you do y.append(x); you are performing an operation on the StringBuffer that is referred to by y and b, which is why b is now "BA".
But when you do y = x you are just assigning the variable x, which is a reference to another StringBuffer, to y which results in this:
|a| ----> |StringBuffer|
+-+ | |
|x| ------------+ |
Please note that not quoting the source of your question is not optional at JavaRanch.
Javaranch Policy wrote:When asking any question, please quote your sources. Quoting your sources means that you mention where you got the question from: for example the title and authors of a book, the name of a mock exam, address of a website where you got the question from etc.
This is especially important in the certification forum, for the posting of mock questions. This is needed for several reasons:
It is polite and professional to do so. Giving attribution is an important habit, and the author of the source would appreciate it.
For mocks, it will help people find the mock exam. For other cases, it will provide people with a source of information. In either case, it will help put your question in context.
It helps you if other people can check that you have interpreted the question (or the information) in the same way as the author intended.
Note: Do not post questions that you got on the actual exam. Those questions are copyrighted and you are not allowed to republish them.
Besides that, note that most books and such materials have copyrights associated with them. Mentioning the source of the question often helps with copyrights.
Also, attribution is always a nice thing.
So please take a moment to mention the source of your question.