14

There are lot of discussions in stackoverflow regarding pass by value and pass by reference. But i want to know what is happening in the following scenario? This page says java is pass by value. Is Java "pass-by-reference" or "pass-by-value"?.

In case of following code, the the element is removed from the removeElement method , it is removing the 5th element from list when i print the list.

public class Load {
    public static void main(String[] args) {


        ArrayList<Integer> list = new ArrayList<Integer>();
        list.addAll(Arrays.asList(1,1,2,3,5,5,13,21));
        removeElement(list);
        System.out.println(list);
    }
        public static void removeElement(List<Integer> list){
            list.remove(5);//removes element at index 5
        }
}

The output of the program is [1, 1, 2, 3, 5, 13, 21].

Can somebody explain how this is pass by value rather than pass by reference?

Community
  • 1
  • 1
alekya reddy
  • 934
  • 1
  • 8
  • 14
  • 2
    It's impossible this isn't a dupe. – Dave Newton Apr 19 '15 at 06:09
  • 9
    Actually, saying that Java is "pass-by-value" is unnecessarily confusing, and it requires twisting the normal meaning of English words. Java has two types of values: primitive types and reference types (objects and arrays). For primitive types, the value is passed. And for reference types, the reference is passed. Some people like to add that this reference is passed "by value" but that doesn't add anything to anyone's understanding of the topic. Because the reference is passed for reference types, you'll see modifications you make to the object back in the caller of the method. – Erwin Bolwidt Apr 19 '15 at 06:18

3 Answers3

9

Java is always pass by value. The value of any variable of type Object is actually a reference. That's why, for example, == is said to be a reference comparison, and you need to use .equals() for comparing Object(s).

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • 2
    So you're saying - for primitive types, it passes the value. And for reference types, it doesn't pass the value of the object, it instead passes the reference of the object. But it passes this reference "by value". It's much simpler to say IMO that for primitive types, the value is passed, and for reference types, the reference is passed. I think Gravity's comment on [ObDuplicate](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) of this question is the best - it depends on what you mean by "reference". – Erwin Bolwidt Apr 19 '15 at 06:14
  • 3
    @ElliottFrisch In fact, it is not the value of an _object_, it is the value of a _variable_. And this is most likely the cause of so much confusion. – Seelenvirtuose Apr 19 '15 at 06:23
  • I don't want to get in a heated debate here but you may want to re-evaluate that statement. The value of a reference is clearly a value. And Objects are referred to by reference. But the value of an Object a reference? I'd say that the value of an object is the aggregate of all its fields, but I don't think there's any official definition of that. – Erwin Bolwidt Apr 19 '15 at 06:24
9

In case of primitives it purely pass by value and in case of Objects it is pass by value of the reference.

When you pass list from the main method to method removeElement() as an argument there is another (copy) reference that is created that points to the same List instance. Any changes made from either of the references will reflect on the same actual instance. However if you assign one of the reference to some new Object the other reference will still point to the same original instance.

Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
4

Although this question asks (again) for the difference between "pass-by-value" and "pass-by-difference", I think there is an underlying misconception that causes so much confusion. This must be resolved:

As a matter of fact a method call passes all parameters by value. As Erwin Bolwidt pointed out in his comments, there are two kinds of types:

  • primitive types
  • reference types

The JLS (§4) states it:

The Java programming language is a statically typed language, which means that every variable and every expression has a type that is known at compile time.
[...]
There are two kinds of types in the Java programming language: primitive types (§4.2) and reference types (§4.3).

When speaking about "pass-by-value", it means that the content of a variable is passed by value when being used as a parameter for a method call. And the content of a reference typed variable is a reference. It is not the object that is referred to by a variable. That's a huge difference.

In your example, the content of the variable list is passed as value to the method removeElement. This value is the reference to the list object.

That further means that the method removeElement now has access to that list object (by its reference) and can manipulate it, for example remove an element. But it has no access to the content of the variable with which the method was called. So it cannot change the content of the variable list inside the main method (which would be possible with "pass-by-reference").

Seelenvirtuose
  • 20,273
  • 6
  • 37
  • 66
  • 1
    It turns out that **in java** pass-by-value is the same as pass-by-reference just because a `value` means (in java) a `reference`. (except for primitive types) – Al.G. Apr 19 '15 at 07:16
  • 1
    No, it is not the same, because unfortunately the word "reference" means a different thing in both contexts. – Seelenvirtuose Apr 19 '15 at 09:42
  • "So it cannot change the content of the variable list inside the main method" - But isn´t that exactly what happens? – 最白目 Apr 06 '18 at 07:23
  • No! As pointed out: It can't happen! I guess you mean that the content of the list object (that is referred to by the variable) is changed. This is different to changing the content of the variable - which is the reference itself. The latter would be the essence of "pass-by-reference" which Java simply doesn't have. – Seelenvirtuose Apr 06 '18 at 07:26