That question has nothing to do with pass by reference (which doesn't exist in Java® anyway) or pass by value. It is about the fact that mutable objects remain mutable. You are sending two StringBuilder references to that method. One of them is re‑assigned, which means that changes in the method affect a local variable with no relationship to the parameter. So the first StringBuilder object remains unchanged. The re‑assigned object is returned and assigned to s3, which not contains "a".
The second object, however, has "b" appended to it, so when you print it out, it displays s2b.
Agree to disagree, the code is not about immutable objects , its about Passing data among methods and there is such a thing as passing by value(copy) or passing by reference in Java.
this line its just creates new StringBuilder this is easy to understand.
the second line it also creates new stringbuilder object, this why is so confuesing.
any other explanations?
Afraid you are mistaken. There is no such thing as pass by reference in Java®.
ubey bey wrote:. . . there is such a thing as passing by value(copy) or passing by reference in Java.
I thought that line is easy to understand. There is nothing unusual about having two StringBuilder objects. I think you need to follow the execution with a pencil and note which object is pointed to by which reference. Also write down the contents of each StringBuilder.
. . . second line it also creates new stringbuilder object, this why is so confuesing.
any other explanations?
However: it's important to understand that variables of non-primitive types in Java are references and that these references are passed by value when you call a method. Note that passing a reference by value is not the same as passing by reference!
I see what you're saying. People telling you Java is pass by reference are right. But the implications to that when the thing being passed is a non-primitive are subtle.
For primitives being passed to a method, a literal copy of the value of that primitive is put on the stack. So the method has it's own copy and changes to it are not reflected outside the scope of the method.
For objects what gets passed is a also copy like people have said, but it's a copy of the pointer to the original object. That copy of that pointer also gets thrown away after the method returns, but there are differences from the case with primitives for the result of using that copy of the pointer to the object.
Here are the implications for the results of your specific program.
1) Pointers to S1 and S2 are passed in to work(), not S1 and S2 themselves.
2) You then take the pointer to S1 you were passed in work() and make it point to something else, a completely new place in memory.
Because that pointer ,which is now pointing to something else, is thrown away when the method returns, nothing you do to that new place in memory is preserved when the method returns.
3) You then take the pointer to S2 you were passed in work() and ACCESS it, that is, follow it to its memory value or, to say it a 3rd way, dereference it.
Now you are mucking around with the same memory that the calling context (main) has. You followed the copy of the pointer to the actual object in memory.
Any changes you make to the values you find there are changes to the same place in memory the calling context (main) sees.
The thing to remember is while Java is pass by value with respect to primitives and objects (non-primitives) that value is a shallow value.
It does not go through an object and copy every value in that object . It is a copy to a pointer to the live object.
When you invoke a method, access a field, use the cast operator, perform String concatonation (what you did) use instanceof , == , != or the conditional operator ? : any of those are done on the actual object itself (which you dereferenced via your copy of the pointer) .
Thaty's why S2 == "S2b".
Which book? Please always tell us the origin for such questions, to avoid copyright problems, and so those of us who have access to that book can read the original. Please include the page number. I hope that book doesn't say that Java® supports pass‑by‑reference.
ubey bey wrote:. . . . THe book explains about passing data by copy or by reference. . . .
its a book of Sybex
title: OCA: Oracle Certified Associate Java SE 8 Programmer I study quide.
By: Jeanne Boyarsky & Scott Selikoff
page 39 Passing data among methods.
The book explaines between primetive and reference types, thats why i thought that it ment pass-by-reference.
on page 39 it explains with example wich ment for other languages like Perl.
But the author explains that Java is pass-by-value language.