Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

String - Pass by Reference!!  RSS feed

 
Mark Henryson
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Basically, I have doubt in Pass by value and pass by reference in Java. Usually if I have doubt, before posting it, I will search for it. Like wise I searched "Pass by value". I got many threads.

But, what a funny thing is in one thread they mentioning if you are passing copy of the object then it is pass by reference and passing copy of value is pass by value.

Some other contradict, everything is pass by value in Java. I read atleast 10 to 15 threads, which creates further confusion than then?!!!

Okay, I will come to the problem, If i am passing the string object as a parameter and inside that method I am changing the value of that string. Outside the method whether the new modified string will display or not. I coded the pgm below, and it's displaying the modified string.

Some of them telling, String are immutable. Whether it is related to my problem ?

 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have doubt in Pass by value and pass by reference in Java.

Java is always pass-by-value.

But, what a funny thing is in one thread they mentioning if you are passing copy of the object then it is pass by reference and passing copy of value is pass by value.
This is incorrect. Objects are not passed at all; their references are. And because the reference values are copied, this makes Java a pass-by-value language.

Some other contradict, everything is pass by value in Java.

Correct.

If i am passing the string object as a parameter and inside that method I am changing the value of that string.
The code isn't doing what you think it's doing...


This code does not modify the string. It simply points y's str reference to a new String object ("Tiger").

Think of it this way. Say you have a piece of paper on which is written the street address of a house. You give me a piece of paper with the same address on it (i.e., a copy). If I then go to the house and change the channel on the television set, you'll see that change when you go to the house.

Now, say I take my piece of paper and write a new address on it. The address on your piece of paper isn't going to change, because I'm working from a copy.

This is analogous to Java, because variables are like the pieces of paper. The address is like the value of an object reference. The house is like the object in question, and so on...

Hope this helps...
[ July 14, 2005: Message edited by: Steve Morrow ]
 
Hentay Duke
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you explain exactly what your confusion is? I'm not sure I understand the question.

Here's a couple answers to possible questions.

Java is always pass by vale.

Strings are immutable. Basically at the end of your program there are three strings in memory.

Edit: I was a bit slow in my response. Steve explains it well above.
[ July 14, 2005: Message edited by: Hentay Duke ]
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It might also help to see a decent definition of pass-by-value and pass-by-reference:

Pass-by-value
- When an argument is passed to a function, the invoked function gets a copy of the original value.
- The local variable inside the method declaration is not connected to the caller's argument; any changes made to the values of the local variables inside the body of the method will have no effect on the values of the arguments in the method call.
- If the copied value in the local variable happens to be a reference (or "pointer") to an object, the variable can be used to modify the object to which the reference points.

Pass by reference (NOT available in Java)
- When an argument is passed to a function, the invoked function gets a reference to the parameter, not a copy of its value.
- Any changes made to the local variable will be reflected by identical changes to the caller's arguments.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To me, saying that Java is always pass-by-value is somewhat misleading. I know this is what the creators of Java call it. However, the above program points out that the object that x refers to is changed inside the f() method. Sure, the reference is copied, but the copy still refers to the original object. This means that any changes to the object via the reference y in the method f() are visible via the reference x in main(). From what I've learned about programming, this seems a lot like pass-by-reference. You may argue that you never pass the object in Java; you always pass references. Well, that sounds a lot like pass-by-reference to me.

In the end, it's all semantics and the language lawyers can call it what they want. For me, it makes sense to call it pass-by-reference.

Layne
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll get along better with the rest of us to say it's passing a reference by value. It's a bit picky, but important. You can change the object that is referenced, but you can't change the callers reference to it.

In true pass by reference, you'd give a called method a reference to your reference, and the called method could change it. So even though strings are immutable this could happen:

Stare at that until it becomes frightening. Every method call would give code you don't control permission to change your variables.
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may argue that you never pass the object in Java; you always pass references. Well, that sounds a lot like pass-by-reference to me.
It's a farily common misconception that passing "a" reference is the same thing as pass-by-reference. It's not. Don't get tripped up on the fact that Java calls its "pointers" "references". The reference's value is copied into the method; that's what makes it pass-by-value. If it were pass-by-reference, the method would receive an alias to the original reference, not a copy.

It's not "language lawyering" at all...

Some people will say incorrectly that objects are passed "by reference." In programming language design, the term pass by reference properly means that when an argument is passed to a function, the invoked function gets a reference to the original value, not a copy of its value. If the function modifies its parameter, the value in the calling code will be changed because the argument and parameter use the same slot in memory. ...The Java programming language does not pass objects by reference; it passes object references by value. Because two copies of the same reference refer to the same actual object, changes made through one reference variable are visible through the other. There is exactly one parameter passing mode -- pass by value -- and that helps keep things simple.

-- Arnold, K., Gosling J., Holmes D. (2000). The Java� Programming Language Third Edition. Boston: Addison-Wesley.
 
Ravi Srinivas
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At the risk of adding confusion unnecessarily...

It might be useful to try and understand why Output 1 differs from Output 2 whereas Output 3 is the same as Output 4 in the code below-

 
Mark Henryson
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


So it will be like this

 
Mark Henryson
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also pls explain about "String pool" if possible
 
Mark Henryson
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are telling that object are not passed by refernce. We are passing the reference by value. That is passing the copy of the reference value. What is the reference value. It is the location in the memory where the data is stored(if i am correct). So the reference value must be some hexadecimal number and it will be unique?!!

If i am passing that reference means any value in that reference if changed it should be reflected and the old value should be vanish.

Correct me if I am wrong....
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!