• Post Reply Bookmark Topic Watch Topic
  • New Topic

Pass-by-reference  RSS feed

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hallo,

I have this question that i dont understand:



results are s1 =s1; s2 = s2b and s3 = a.

I dont understand why is s2 = s2b?

 
Marshal
Posts: 56819
173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post real code which will actually compile, without abbreviations like sout.
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.
 
ubey bey
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?



 
Campbell Ritchie
Marshal
Posts: 56819
173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

ubey bey wrote:. . . there is such a thing as passing by value(copy) or passing by reference in Java.

Afraid you are mistaken. There is no such thing as pass by reference in Java®.

. . .  second line it also creates new stringbuilder object, this why is so confuesing.

any other explanations?

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.
 
Java Cowboy
Sheriff
Posts: 16077
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campell is right, there is no such thing as pass-by-reference in Java. There is only pass-by-value.

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!
 
Ranch Foreman
Posts: 3128
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

ubey bey wrote:
I dont understand why is s2 = s2b?



Track through the code just for s2.
The constructor initialises it to "s2", then it is passed into the work method as the second parameter (b).
In the work method b.,append("b") is called.
 
Ranch Hand
Posts: 67
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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".


 
Dave Tolls
Ranch Foreman
Posts: 3128
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

David Simkulette wrote:

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.



I think you mean "Java is not pass by reference".
 
ubey bey
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks David,

now i understand, i have folowd the s2 object by drawing with pencil before Davids explanation. THe book explains about passing data by copy or by reference. When you folow those rules and with this code is good example to get confuesd.
 
David Simkulette
Ranch Hand
Posts: 67
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:

I think you mean "Java is not pass by reference".



LOL Yep that's what I meant. LOL.
 
Campbell Ritchie
Marshal
Posts: 56819
173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

ubey bey wrote:. . . . THe book explains about passing data by copy or by reference. . . .

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
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.

But the author explains that Java is pass-by-value language.

on page 39 it explains with example wich ment for other languages like Perl.
 
Campbell Ritchie
Marshal
Posts: 56819
173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!