Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Pass by reference / pass by value

Reply
Thread Tools

Pass by reference / pass by value

 
 
Jerry
Guest
Posts: n/a
 
      08-22-2005
Is that true that Java pass primitive type by value when calling a
method and pass object by reference?

If I pass a StringBuffer object to a method and append the strings to
the StringBuffer object in the called the method. When the method
returns, the original StringBuffer object in the calling method will be
modified. Is that right?

Thanks a lot!

 
Reply With Quote
 
 
 
 
jan V
Guest
Posts: n/a
 
      08-22-2005
> Is that true that Java pass primitive type by value when calling a
> method and pass object by reference?


No. You could read any book on Java (the language), or the Java Language
Spec, and both would tell you everything is passed by value.

> If I pass a StringBuffer object to a method and append the strings to
> the StringBuffer object in the called the method. When the method
> returns, the original StringBuffer object in the calling method will be
> modified. Is that right?


That's correct



 
Reply With Quote
 
 
 
 
Monique Y. Mudama
Guest
Posts: n/a
 
      08-22-2005
On 2005-08-22, jan V penned:
>> Is that true that Java pass primitive type by value when calling a
>> method and pass object by reference?

>
> No. You could read any book on Java (the language), or the Java
> Language Spec, and both would tell you everything is passed by
> value.
>
>> If I pass a StringBuffer object to a method and append the strings
>> to the StringBuffer object in the called the method. When the
>> method returns, the original StringBuffer object in the calling
>> method will be modified. Is that right?

>
> That's correct
>


You're trying to be purposely vague, aren't you?

To the OP: Jan is right. IIRC it's because you're not actually
passing an Object; you're passing a reference, which is passed by
value.

There. Clear as mud.

--
monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      08-22-2005
On 22 Aug 2005 12:00:08 -0700, "Jerry" <(E-Mail Removed)> wrote
or quoted :

>Is that true that Java pass primitive type by value when calling a
>method and pass object by reference?


see http://mindprod.com/jgloss/callbyreference.html
http://mindprod.com/jgloss/callbyvalue.html
 
Reply With Quote
 
Kenneth P. Turvey
Guest
Posts: n/a
 
      08-22-2005
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jerry wrote:

> Is that true that Java pass primitive type by value when calling a
> method and pass object by reference?


Yes, exactly. Note that arrays are object types in Java.

>
> If I pass a StringBuffer object to a method and append the strings to
> the StringBuffer object in the called the method. When the method
> returns, the original StringBuffer object in the calling method will be
> modified. Is that right?


Yes.

- --
Kenneth P. Turvey <(E-Mail Removed)>

Currently seeking employment as a Java developer in the St. Louis area.


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)

iD8DBQFDCjo23naBnF2rJNURAhIcAKC/+86LMfHE8qf5CT6JWTZTQMonpwCgsqif
9RjUgSOcvAmq7LjppZWZOmY=
=dBsO
-----END PGP SIGNATURE-----
 
Reply With Quote
 
John C. Bollinger
Guest
Posts: n/a
 
      08-23-2005
Kenneth P. Turvey wrote:
> Jerry wrote:
>>Is that true that Java pass primitive type by value when calling a
>>method and pass object by reference?


> Yes, exactly. Note that arrays are object types in Java.


No, not exactly. As others have already pointed out, Java has only pass
by value. Also mentioned, but perhaps not quite as clear, is that Java
doesn't pass objects at all, only references to objects (by value).
Indeed, in Java you can *never* put your figurative hands directly on an
Object: you can only manipulate objects via references. We are
typically sloppy in our terminology on this point: we say we have a
variable of type Foo or an array of Bar, when what we really have is a
variable of type *reference to Foo* or an array of *references to Bars*.
Similarly, we often say that we pass a Baz, when we really mean that
we pass a reference to a Baz -- by value.

So what's the difference between passing an object by reference and
passing a reference to it by value? It is a subtle distinction. The
key difference is what happens if you assign to the parameter in which
the argument was passed. Consider:

void doSomething(StringBuffer sb) {
sb.append("How many roads must a man walk down?");
sb = new StringBuffer("So long, and thanks for all the "); //*
sb.append("fish");
}

void doSomethingElse() {
StringBuffer buf = new StringBuffer("");

doSomething(buf);
System.out.println(buf);
}

When doSomethingElse() is invoked, what do you expect it to print?

With pass by value, at the line marked (//*) above, all that happens is
a reference to the new StringBuffer is assigned to the local variable
"sb", which happens to have been initialized with the StringBuffer
reference passed by the caller. This is quite invisible to the caller.
Since the buffer was previously appended to via the reference passed
by the caller, on return the caller sees the first append reflected via
its copy of the reference, and prints "How many roads must a man walk
down?". This is indeed what happens.

With pass by reference, however, the marked assignment would change the
value of buf in doSomethingElse(), so that the original StringBuffer is
lost. The second append would be visible, so the method would print
"So long, and thanks for all the fish". This is not what happens.

--
John Bollinger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Chris Berg
Guest
Posts: n/a
 
      08-23-2005
On 22 Aug 2005 12:00:08 -0700, "Jerry" <(E-Mail Removed)>
wrote:

>Is that true that Java pass primitive type by value when calling a
>method and pass object by reference?


This has been discussed sooo many times here that I concider it really
just a brain fitness exercise to try to mix in with the discussion.
(Just try this link:
http://groups.google.com/groups?q=reference+value+java , around 70.000
refs)

But for the fun of it:

IMO, all the trouble with this topic comes from the fact that 'pass by
reference' and 'pass by value' are terms from other languages,
specifically C, where they are well-defined and easily understood.
It's wrong to use them at all in Java when referring to objects
(primitives is a different matter - 'pass by value' is quite suitable
here, although there is a little twist with arrays).

The reason that the C-terms cannot be used is that object variables
are declared and created differently than in C / C++. In C you can
declare a variable as a struct, or as a struct*. This option does not
exist in Java, well, at least not directly.

One example of what happens is that you cannot make a swap method.
This won't work, of course:

void swap (int a, int b){
int c = a;
a = b;
b = c;
}

I find this a bit of a nuissance sometimes, when a method wants to
return more than one scalar to the caller; then you have to create a
class to hold them.

But alas - who gets everything his own way?

Chris
 
Reply With Quote
 
Jaakko Kangasharju
Guest
Posts: n/a
 
      08-23-2005
Chris Berg <(E-Mail Removed)> writes:

> IMO, all the trouble with this topic comes from the fact that 'pass by
> reference' and 'pass by value' are terms from other languages,
> specifically C, where they are well-defined and easily understood.


No, both terms are much older than C, and apply to programming
languages in general. Besides, C, like Java, does not even have pass
by reference. Both use pass by value always.

> It's wrong to use them at all in Java when referring to objects


That's just because you cannot pass an object to a method at all, so
parameter passing conventions do not apply to them.

--
Jaakko Kangasharju, Helsinki Institute for Information Technology
If you want a response, you must demonstrate
familiarity with ordinal arithmetic
 
Reply With Quote
 
Chris Berg
Guest
Posts: n/a
 
      08-23-2005
On Tue, 23 Aug 2005 11:10:54 +0300, Jaakko Kangasharju
<(E-Mail Removed)> wrote:
>
>That's just because you cannot pass an object to a method at all, so
>parameter passing conventions do not apply to them.


After teaching Computer Science and java programming for years, I have
come to the conclusion that the 'pass by ...' term is not the right
way to make students understand the topic.

Real understanding comes when you open the lid and reveal the
technical details: That pointers are indeed present in Java (how else
would you explain 'NullPointerException' ?). As soon at the student
realises that a declaration of an object is in fact just a placeholder
for an address (or a reference, if you wish), initially pointing
nowhere, then everything falls into place, and the difference between
for instance 'myString=="abc" and "myString.equals("abc") becomes
intuitively clear. Then it is no problem to understand that all formal
method parameters (scalars and objects alike) are copies of the actual
ones.

Also, it seems that students with no prior programming experience find
it much easier to comprehend this topic than old-timers, who tend to
fit new knowledge into already known patterns.

I once met a senior programmer (Fortran) who asked me what the 'RAM'
was actually used for. The whole idea that a variable in fact occupies
one or more consecutive cells in the 'RAM', and that a pointer to the
variable is in fact the (more or less physical) address of the first
one of these RAM cells never crossed his mind. And he was certainly
not stupid!!
 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      08-23-2005
Chris Berg wrote:
....
> IMO, all the trouble with this topic comes from the fact that 'pass by
> reference' and 'pass by value' are terms from other languages,
> specifically C, where they are well-defined and easily understood.
> It's wrong to use them at all in Java when referring to objects
> (primitives is a different matter - 'pass by value' is quite suitable
> here, although there is a little twist with arrays).

....

I learned both terms, in a programming languages class, in the early
1970's, years before I even heard of C. If I remember correctly, they
were illustrated with examples from Algol and Snobol. They are two of a
set of technical terms for describing calling conventions. Different
languages support different combinations of calling conventions. See
http://en.wikipedia.org/wiki/Paramet...ter_science%29 for a list
that includes all the ones I've ever heard of.

I don't see anything wrong with discussing any calling convention in
connection with any language: "Java does not support call-by-name." is a
meaningful, and true, sentence.

Patricia
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Variable Input on procedure - pass by value or pass by reference? Reuven VHDL 0 03-19-2009 07:21 PM
Re: Variable Input on procedure - pass by value or pass by reference? Tricky VHDL 0 03-19-2009 03:59 PM
Variable Input on procedure - pass by value or pass by reference? Tricky VHDL 0 03-19-2009 03:58 PM
Problem understanding pass by value and pass by reference of arrays and what happens in the memory venkatagmail C++ 11 10-03-2007 02:00 PM
Passing the value by reference is same as pointer by reference sam pal C++ 3 07-16-2003 09:14 PM



Advertisments