Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #18608 > unrolled thread

Objects only passed by copy?

Started byLuft <edamron@spamcop.net>
First post2012-09-08 17:22 -0700
Last post2012-09-30 13:50 -0700
Articles 7 — 5 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Objects only passed by copy? Luft <edamron@spamcop.net> - 2012-09-08 17:22 -0700
    Re: Objects only passed by copy? Arne Vajhøj <arne@vajhoej.dk> - 2012-09-08 20:59 -0400
      Re: Objects only passed by copy? Arne Vajhøj <arne@vajhoej.dk> - 2012-09-08 21:02 -0400
    Re: Objects only passed by copy? Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-09-08 21:30 -0400
      Re: Objects only passed by copy? Lew <lewbloch@gmail.com> - 2012-09-30 17:23 -0700
    Re: Objects only passed by copy? Roedy Green <see_website@mindprod.com.invalid> - 2012-09-08 19:57 -0700
      Re: Objects only passed by copy? Roedy Green <see_website@mindprod.com.invalid> - 2012-09-30 13:50 -0700

#18608 — Objects only passed by copy?

FromLuft <edamron@spamcop.net>
Date2012-09-08 17:22 -0700
SubjectObjects only passed by copy?
Message-ID<6e06860b-4c72-4f4b-8892-69aa4242a4dc@googlegroups.com>
I'm learning JAVA and read that EVERYTHING in JAVA is passed by copy and that in the case of objects the internal variables share a reference but that objects are also passed by copy.

I'm trying to think of a piece of code that would demonstrate this but can't think of anything off the top of my head.

Thanks.

[toc] | [next] | [standalone]


#18609

FromArne Vajhøj <arne@vajhoej.dk>
Date2012-09-08 20:59 -0400
Message-ID<504be9dd$0$292$14726298@news.sunsite.dk>
In reply to#18608
On 9/8/2012 8:22 PM, Luft wrote:
> I'm learning JAVA and read that EVERYTHING in JAVA is passed by copy and that in the case of objects the internal variables share a reference but that objects are also passed by copy.
>
> I'm trying to think of a piece of code that would demonstrate this but can't think of anything off the top of my head.

References to objects are passed by value.

You can not really pass an object itself at all.

Arne

[toc] | [prev] | [next] | [standalone]


#18610

FromArne Vajhøj <arne@vajhoej.dk>
Date2012-09-08 21:02 -0400
Message-ID<504beaa6$0$292$14726298@news.sunsite.dk>
In reply to#18609
On 9/8/2012 8:59 PM, Arne Vajhøj wrote:
> On 9/8/2012 8:22 PM, Luft wrote:
>> I'm learning JAVA and read that EVERYTHING in JAVA is passed by copy
>> and that in the case of objects the internal variables share a
>> reference but that objects are also passed by copy.
>>
>> I'm trying to think of a piece of code that would demonstrate this but
>> can't think of anything off the top of my head.
>
> References to objects are passed by value.
>
> You can not really pass an object itself at all.

Example:

public class RefByVal {
	public static void demo(StringBuilder sb) {
		sb.append("b");
		sb = new StringBuilder("c");
	}
	public static void main(String[] args) {
		StringBuilder sb = new StringBuilder("a");
		demo(sb);
		System.out.println(sb.toString());
	}
}

Arne

[toc] | [prev] | [next] | [standalone]


#18611

FromEric Sosman <esosman@ieee-dot-org.invalid>
Date2012-09-08 21:30 -0400
Message-ID<k2grgj$n3$1@dont-email.me>
In reply to#18608
On 9/8/2012 8:22 PM, Luft wrote:
> I'm learning JAVA and read that EVERYTHING in JAVA is passed by copy and that in the case of objects the internal variables share a reference but that objects are also passed by copy.

     Argument expressions provide values, those values initialize
method or constructor parameters.  In that sense "passed by copy"
is correct ("passed by value" is the more usual phrase): The
parameter gets a copy of the value of the argument expression,
not the expression itself.  That is why you can call a method
like `something(x + 3)' or `otherthing(obj.getAnswer())', even
though you cannot write `x + 3 = 9' or `obj.getAnswer() = "NO"'.

     Some people (and some writers of books) like to say that
primitive values (int, double, and so on) are passed by value,
while objects (String, JButton, et al.) are passed by reference.
I think this is needlessly confusing -- and it still doesn't
explain `otherthing(obj.getAnswer())'.  To my mind, it is more
useful to think of everything being passed by value, where a
"value" may be

     - a primitive value like 5 or 98.6 or 'X' or false, or

     - a reference value that is a "handle" for some object
       instance (or is null).

With this point of view many things that are complicated in the
"pass by value or sometimes by reference" scheme become much
simpler: You *always* pass by value, whether the value is a
primitive or a reference.

     With either viewpoint objects are not "passed by copy," by
which I mean that the object inside the method is not an independent
copy of the object provided by the caller.  There's just one object,
and the method gets a copy of the *reference* to that object -- and
that leads to the example below.

> I'm trying to think of a piece of code that would demonstrate this but can't think of anything off the top of my head.

     Okay.  Let's write a method that appends 'X' to a StringBuilder:

	static void appendX(StringBuilder builder) {
	    builder.append('X');
	}

Simple enough, right?  Fine, now let's call it:

	StringBuilder adage = new StringBuilder(
	    "Better late than never.");
	System.out.println(adage);  // prints the old saying
	appendX(adage);
	System.out.println(adage);  // prints ... what?

Question: After the call, does the StringBuilder instance that
`adage' refers to contain an 'X' or not?

     If the StringBuilder *instance* is copied, appendX() has
its own independent StringBuilder to play with.  It can append
its 'X', or append twenty 'X'es, or insert an 'X' in the middle
of what's already there -- but the StringBuilder in the caller,
the one `adage' refers to, will be unchanged because appendX()
is using a copy, not the original.

     If the StringBuilder *reference* is copied, appendX() has
a reference to the same StringBuilder instance that `adage'
designates.  When appendX() adds its 'X', it changes the content
of that one single StringBuilder instance, and the caller will
now find an 'X' on the end of `adage'.

     Which is it?  Make your prediction before trying the code,
then try the code and check your answer.

-- 
Eric Sosman
esosman@ieee-dot-org.invalid
"The speed at which the system fails is usually not important."

[toc] | [prev] | [next] | [standalone]


#19003

FromLew <lewbloch@gmail.com>
Date2012-09-30 17:23 -0700
Message-ID<3d87fda0-481b-4692-90ae-6ab5fd90975d@googlegroups.com>
In reply to#18611
Eric Sosman wrote:
. . . 
>      Some people (and some writers of books) like to say that
> primitive values (int, double, and so on) are passed by value,
> while objects (String, JButton, et al.) are passed by reference.

Which is incorrect.

> I think this is needlessly confusing 

and wrong 

> -- and it still doesn't explain `otherthing(obj.getAnswer())'.  
> To my mind, it is more
> useful to think of everything being passed by value, where a

which is correct 

> "value" may be
>      - a primitive value like 5 or 98.6 or 'X' or false, or
>      - a reference value that is a "handle" for some object
>        instance (or is null).

This is the correct view, and the one looked for on, e.g., the 
Java certification tests.

> With this point of view many things that are complicated in the
> "pass by value or sometimes by reference" scheme become much
> simpler: You *always* pass by value, whether the value is a
> primitive or a reference.

This is the correct view.

>      With either viewpoint objects are not "passed by copy," by

That is correct. It is the reference that is copied.

It is like a person with a P.O. box and a street address. A letter 
addressed to that person reaches the same entity regardless of the 
pointer used.

Remember, a Java reference is a pointer.

> which I mean that the object inside the method is not an independent
> copy of the object provided by the caller.  There's just one object,
> and the method gets a copy of the *reference* to that object

i.e., the pointer to the object, 

> -- and that leads to the example below.

>> I'm trying to think of a piece of code that would demonstrate this 
>> but can't think of anything off the top of my head.
> 
>      Okay.  Let's write a method that appends 'X' to a StringBuilder:
> 
> ... [excellent pedagogical example] ...
> 
> Question: After the call, does the StringBuilder instance that 
> `adage' refers to contain an 'X' or not? 
> 
> ... [excellent pedagogical example] ...
> 
>      Which is it?  Make your prediction before trying the code,
> then try the code and check your answer.

-- 
Lew

[toc] | [prev] | [next] | [standalone]


#18615

FromRoedy Green <see_website@mindprod.com.invalid>
Date2012-09-08 19:57 -0700
Message-ID<ob1o485fcnj7gdhi596om6dghp4adfun8h@4ax.com>
In reply to#18608
On Sat, 8 Sep 2012 17:22:05 -0700 (PDT), Luft <edamron@spamcop.net>
wrote, quoted or indirectly quoted someone who said :

>I'm learning JAVA and read that EVERYTHING in
 JAVA is passed by copy and that in the case of objects the internal
variables share a reference but that objects are also passed by copy.
>
>I'm trying to think of a piece of code that would demonstrate
 this but can't think of anything off the top of my head.

 that is not correct. Objects are not cloned.  References to single
object as passed around. 

Write a method that takes an object parameter with a mutable field.
Change it in the method.  If it copied, you would see the mother
object changed as well. You won't.

see http://mindprod.com/jgloss/object.html
and follow links

This drives you crazy as a newbie, but once you get it is seems so
easy and so obvious, it is hard to understand why you had so much
trouble.  
-- 
Roedy Green Canadian Mind Products http://mindprod.com
A new scientific truth does not triumph by convincing its opponents and making them see the light,
but rather because its opponents eventually die, and a new generation grows up that is familiar with it.
~ Max Planck 1858-04-23 1947-10-04 

[toc] | [prev] | [next] | [standalone]


#18999

FromRoedy Green <see_website@mindprod.com.invalid>
Date2012-09-30 13:50 -0700
Message-ID<1sbh68loqq65n3c1f4jfl0n4h15gg0a45l@4ax.com>
In reply to#18615
On Sat, 08 Sep 2012 19:57:04 -0700, Roedy Green
<see_website@mindprod.com.invalid> wrote, quoted or indirectly quoted
someone who said :

>Write a method that takes an object parameter with a mutable field.
>Change it in the method.  If it copied, you would see the mother
>object changed as well. You won't.
Let me try that again:

When you call a method with a object parm, a copy of the reference
gets passed.  The object itself is not cloned.

You can verify this:

If the object has a mutable field, and you changed the field in the
method, you would discover the caller's object field is changed too.

If you changed the reference to the object in the method to point to
some other object, you would discover the caller is still pointing to
the same object.
-- 
Roedy Green Canadian Mind Products http://mindprod.com
The iPhone 5 is a low end Rolex. 

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web