• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Reference  RSS feed

 
Shannon Sims
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,
Having a C background, is everything passed by reference not by value in Java? If so, would you ever want to pass something by value and if you wanted to, how would you go about it?

Thanks!
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Everything is passed by value.

All variables of object type are pointers.

So if you call a method that takes an int, the value of the int is copied into the argument.

If you call a method that takes a String, the pointer to the String is copied.

See here for a more in-depth look at this answer.
 
Shannon Sims
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the article...everything is passed by value, objects pass a copy of the reference and a primitives pass a copy of the value, correct?
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shannon Sims:
Hello All,
Having a C background, is everything passed by reference not by value in Java? If so, would you ever want to pass something by value and if you wanted to, how would you go about it?

Thanks!


There is much heated debate whether Java has pass by reference at all. Most enthusiasts say that everything is passed by value. This topic has been discussed here on many occassions. You should use the Search tool to see what others have said about it. A recent discussion in the intermediate forum covered it rather thoroughly.

Layne
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Layne, there is no debate.

Java is pass by value only, always. What some people claim to be pass by reference is actually references being passed by value.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeroen Wenting:
Layne, there is no debate.

Java is pass by value only, always. What some people claim to be pass by reference is actually references being passed by value.

<stirring the pot>
Well, that depends on what you mean by "actually".
</stirring the pot>
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just in case anyone is still confused on this matter, I created a little Flash application some time back to illustrate how parameters are passed in Java. You can check it out here.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Layne, there is no debate.


OHHH!!! Let's start a debate about whether there's a debate!!!

While i agree there SHOULD be no debate, i think i still occasionally meet people who WILL debate it.
[ March 31, 2005: Message edited by: fred rosenberger ]
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
If you call a method that takes a String, the pointer to the String is copied.


Now, now, Ernest. Java doesn't have pointers!!! Let's call a reference a reference and be done with it.

Originally posted by fred rosenberger:
OHHH!!! Let's start a debate about whether there's a debate!!!


<SLAP>

But seriously, the answer lies in the fact that objects are never passed in Java. Only references and primitives are passed. In C you can pass Objects by value and pointer to objects by value (a technique that the C people call pass-by-reference, but C++ people disagree). In C++ you can pass objects by value, pass pointers to objects by value, and pass objects by reference (a technique quite different from passing pointers by value). See my examples in the thread mentioned by Layne for how these would function differently.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joel wrote:
<SLAP>


Dang!!! i got smacked DOWN on the ranch!!!

(of course, i deserved it!!!)
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quote by Layne in the other thread:

Perhaps we can clarify this with an example in a language that uses "proper pass-by-reference". In my experience with other languages, primarily with C and C++, pass-by-reference works identically with the way we see it working in Java as we are discussing here.


C++'s pass by reference is quite different from C's.



I's done work in C++ for years without knowing that the third option existed; I had only used the second option and thought that I was passing-by-reference, when C++ uses that term to mean something completely different!
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by fred rosenberger:


OHHH!!! Let's start a debate about whether there's a debate!!!

While i agree there SHOULD be no debate, i think i still occasionally meet people who WILL debate it.


Sure they'll raise some objections to the general knowledge (as laid down in the JLS), but I wouldn't call that debate

Of course the exact definition of what constitutes a debate is open for debate...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!