1

Possible Duplicate:
Is Java “pass-by-reference”?

Maybe I'm missing something...but I can't really understand why Java uses both the pass-by-value and pass-by-reference. Why not using only one paradigm?

Community
  • 1
  • 1
Geims Bond
  • 19
  • 1

2 Answers2

9

It doesn't. Java is purely pass-by-value. The value passed when you're dealing with an object is an object reference, but that has nothing to do with "pass by reference."

Pass-by-reference means that a function can be passed a variable and change the contents of that variable in the calling function. That does not exist in Java.

So for instance:

void foo() {
    int a;

    a = 42;
    bar(a);
    System.out.println(a); // Will ALWAYS be 42
}

void bar(int b) {
    b = 67;
}

Contrast with C++, which does have pass-by-reference:

// C++ code
void foo() {
    int a;

    a = 42;
    bar(a);
    cout << a; // 67?!
}
void bar(int& a) { // <== Note the &
    a = 67;
}

Java has no equivalent of the & in C++ (or out / ref in C#).

You're probably thinking of object references, which is a completely separate use of the word "reference" than the "reference" in pass-by-reference. Let's look at an example with objects:

void foo() {
    Object o1, o2;

    o1 = new Object();
    o2 = o1;
    bar(o1);
    System.out.println(o1 == o2); // Will ALWAYS be true
}

void bar(Object o) {
    o = new Object();
}

If Java had pass-by-reference (and we'd used it to pass the object variable to bar), the == in foo would be false. But it isn't, and there is no way to make it so.

The object reference allows you to change the state of the object passed into the function, but you cannot change the variable that contained it in the calling function.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • There's a difference, but tt's not **completely separate**: a function can indeed change the object that it was passed. – Marko Topolnik Dec 19 '12 at 12:15
  • 1
    @MarkoTopolnik: No, it is *completely* separate. In pass-by-reference, the "reference" is a reference to **a variable**. What Java passes is a value that refers to an object. Completely and totally different things. – T.J. Crowder Dec 19 '12 at 12:17
  • They are also **related** by the property that the passed-in entity can be mutated, and the effect observed by the caller. In Java you mutate **instance** variables; in pass-by-reference you can mutate **local** variables. They are "completely and totally different" only from a very narrow-minded perspective. – Marko Topolnik Dec 19 '12 at 12:22
  • @MarkoTopolnik: I don't want to get into a long discussion on the topic, but there's a *fundamental* difference: You don't change the state of a variable in pass-by-reference, you clobber its value entirely and replace it with a new value. Object references let you modify the state of the object, but not completely wipe *the object itself* out and replace it. :-) (And could we leave terms like "narrow-minded" out of a rational exchange, please?) – T.J. Crowder Dec 19 '12 at 12:38
  • Don't get me wrong, I fully agree on the "fundamental" aspect! I just argue that "fundamental difference" =/= "completely and totally unrelated". BTW in Java you can also "wipe out the object", just not the one that was passed in, but one that an instance variable refers to. It's really just about going one step deeper into what you can change. About "narrow-minded", read "narrow" instead if it brings in unintended accusations :) – Marko Topolnik Dec 19 '12 at 12:41
  • @MarkoTopolnik: Fair 'nuff. :-) I'm not getting your point about completely wiping out the object, though; instance members aren't part of the pass-by scenario, which involves passing something into a method via its argument list. Instance members don't come into it. – T.J. Crowder Dec 19 '12 at 12:44
  • Imagine a use case that is covered in C++ by pass-by-reference. The same use case can be replicated in Java by a wrapper object, treating the wrapper's instance variable as the local variable in C++. `int[] x = {1}; passByRef(x);`. – Marko Topolnik Dec 19 '12 at 12:46
  • @MarkoTopolnik: Sure, of course, it's the classic work-around. But the point is that you can't wipe out the array, just its contents. You can't completely swap it out, you can only change its state, as I said. – T.J. Crowder Dec 19 '12 at 12:48
  • And, obviously, no contention from me on the semantics of the constructs, but at a "forrest" level, it's about the use cases, and not about the specifics of language features. – Marko Topolnik Dec 19 '12 at 12:50
  • @MarkoTopolnik: Sure. It's just, I'm answering a question about the specifics of language features. :-) *(Why I answered it, I couldn't tell you, given it was a duplicate -- but the way it was phrased was different from what I'd seen before...)* :-) Anyway, I think we're not that far apart, your objection to my wording is noted, thanks. Best, – T.J. Crowder Dec 19 '12 at 12:51
-1

Java is always "Pass By Value". So primitives as well as Object's (reference) both are passed by value.

Edit

Yeah, objects are not passed directly, we always refer them with their reference. So Object's reference is passed by value.

rai.skumar
  • 10,309
  • 6
  • 39
  • 55