• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

StringBuffer example doubt

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the given code, according to me answer should be A,A. But correct answer is A,BA.
Can anybody help?

 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sarika, please QuoteYourSources.
 
Sarika Chavan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.


 
Tim Cooke
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, my mistake, I meant y and b.

When you declare a and b as a StringBuffer you have this. Where the variables a and b are references to the StringBuffer objects.

+-+       +------------+
|a| ----> |StringBuffer|
+-+       +------------+

+-+       +------------+
|b| ----> |StringBuffer|
+-+       +------------+

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|
+-+       +------------+
                ^
+-+             |
|x| ------------+
+-+

+-+       +------------+
|b| ----> |StringBuffer|
+-+       +------------+
                ^
+-+             |
|y| ------------+
+-+

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| ------------+ |
+-+               |
                  |
+-+               |
|y| --------------+
+-+

+-+       +------------+
|b| ----> |StringBuffer|
+-+       +------------+

 
Sarika Chavan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much Tim Cooke.
Now it's clear..
 
Tim Cooke
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excellent. You are welcome.
 
Chan Ag
Rancher
Posts: 1090
14
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sarika,

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.

Chan.
 
Sarika Chavan
Greenhorn
Posts: 21
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for not giving source of my question.

I got this from one of the mock exams.

URL: http://www.javabeat.net/ocpjp-6-mock-exam-java-21/

Question no. 5
 
reply
    Bookmark Topic Watch Topic
  • New Topic