0

Possible Duplicates:
Does Java pass by reference?
Is Java pass by reference?

Hey,

Is it true that Java passes everything by value, and we can't pass something by reference ?

Thanks.

Community
  • 1
  • 1
VisaMasterCard
  • 1,666
  • 4
  • 18
  • 22
  • 1
    I'll bet if you typed that exact phrase into Google, you would have found a ton of links answering your question. – Marc W Jan 27 '11 at 00:43
  • 2
    Biziclop: Java is always always pass by value. Even for primitives. Changing the value of a primitive passed into the method does not retain effect after the method returns. – Marc W Jan 27 '11 at 00:44
  • @Marc W Hmmm, doesn't pass by value mean that a copy is pushed on the stack? – biziclop Jan 27 '11 at 00:46
  • @Biziclip: Not necessarily. All it means is that changes to the value don't disappear after the method returns. Java treats objects as references so the address of the object is passed by value, but you can still modify its fields. You just can't reassign the argument to a new object entirely and have it stick after return. – Marc W Jan 27 '11 at 00:47
  • 2
    Well, apparently pass by reference doesn't mean passing a reference. You live and learn. :) – biziclop Jan 27 '11 at 00:47
  • Copy of what? The object? Nope, you're missing it entirely biziclop. Couldn't be more wrong. You put a reference to the object that lives out on the heap onto the stack. – duffymo Jan 27 '11 at 00:48
  • possible duplicate of [Is Java pass by reference?](http://stackoverflow.com/questions/40480/is-java-pass-by-reference) - highly voted answer here – Bert F Jan 27 '11 at 00:49
  • @Marc W Thanks for the explanation. I know exactly how Java references work, I just wasn't aware that pass by reference meant something different. English isn't my first language, so it happens sometimes. – biziclop Jan 27 '11 at 00:50
  • It's all good. =) All the terms do tend to get annoyingly twisted around. – Marc W Jan 27 '11 at 00:51
  • @duffymo No, I was 100% right. Pass by value does mean that a copy is pushed onto the stack. A copy of the reference in this case, but a copy nevertheless. – biziclop Jan 27 '11 at 00:51
  • Copy of - what? Not the object itself. The reference? Yes. Your original comment didn't spell out which one. If you're so 100% right, and sure of what's happening, then why the question? – duffymo Jan 27 '11 at 00:53
  • @duffymo Because I was confused as to what the difference between pass-a-reference and pass-by-reference is, but pass-by-value was never a question. – biziclop Jan 27 '11 at 00:55
  • I do pass by value in Java 5+ using Atomic* variables (http://download.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/atomic/AtomicInteger.html). It's a bit ugly but it works. The alternative way is an array with one slot. – Chris Dennett Jan 27 '11 at 00:56

3 Answers3

8

Good God, it can't be asked again. Java is pass by value - always, in all cases. That's it.

Here's a reference that quotes James Gosling, who should be authoritative enough for anyone:

From the authors of Java: "There is exactly one parameter passing mode in Java - pass by value - and that helps keep things simple." The Java Programming Language, 2nd ed. by Ken Arnold and James Gosling, section 2.6.1, page 40, 3rd paragraph.

duffymo
  • 305,152
  • 44
  • 369
  • 561
1

Java passes value of references of the objects you are passing and simple value for primitive types. See following discussion on this: Is Java pass by reference?

Community
  • 1
  • 1
Mahesh
  • 611
  • 9
  • 16
1

Java passes primitives by value by default, and all types have their object references passed by value. See http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.3.1 for details.

Shan Plourde
  • 8,528
  • 2
  • 29
  • 42