0

I know this question apparently has many duplicates like here and here.

My question is different though.

Consider the following example:

public class MyClass {
    public static void test(int a, int b) {
        System.out.println("In test() at start: "+a+" "+b);
        int temp=a;
        a=b;
        b=temp;
        System.out.println("In test() at end: "+a+" "+b);
    }

    public static void main(String args[]) {
        int a=1, b=2;
        System.out.println("a: "+a+" b: "+b);

        test(a, b);
        System.out.println("a: "+a+" b: "+b);
    }
}

The output that I get for the above snippet is:

a: 1 b: 2
In test() at start: 1 2
In test() at end: 2 1
a: 1 b: 2

This shows that the original values of a and b in main() have not been swapped when I called test(), thereby implying (if I understand correctly) that it was passed by value.

Now, consider the following code snippet:

public class MyClass {
    public static void test(int[] arr) {
        System.out.println(arr[2]);
        arr[2]=20;
        System.out.println(arr[2]);
    }

    public static void main(String args[]) {
        int[] arr={0,1,2,3,4,5};
        System.out.println(arr[2]);

        test(arr);
        System.out.println(arr[2]);
    }
}

The output that I get for this code snippet is:

2
2
20
20

This shows that the value at arr[2] was changed in the original array in main(), thereby denoting (if I understand correctly) that the array was passed by reference.

Could someone please point out what is going on? Why does it show different behaviors?

Thanks!

  • All arrays are passed be reference, by default variables are passed by value. If you were to use `Integer` it would behave similarly – Mitch May 25 '18 at 02:15
  • I come from a C++ background. So, if some one explains from _that perspective_ it would be very helpful for me. –  May 25 '18 at 02:15
  • @Mitchel0022, okay, got your point. `Integer` as the datatype of a variable or an array element, you mean? –  May 25 '18 at 02:25
  • Either, `Integer` is a object, therefore it will be passed by reference and changing it in the function will change it permanently. `int` will only change in the scope of the function – Mitch May 25 '18 at 02:26
  • @Mitchel0022, okay. So to conclude, arrays and objects are passed by reference; everything else by value. What about containers? I think reference again (because they contain nothing but objects)? –  May 25 '18 at 02:27
  • Containers are reference – Mitch May 25 '18 at 02:28
  • Thank you, @Mitchel0022. It is very helpful for someone who comes from a C++ background like me. If you convert it into an answer, I would be glad to accept it. –  May 25 '18 at 02:29
  • Its not large enough of a question to justify an answer, but glad I could help – Mitch May 25 '18 at 02:30
  • @StaticBeagle, guess what, the links that I have included at the beginning of the question is the exact same that you mention (maybe you didn't check those). And, my _entire post_ explains how it is different than that link. Do you still think that it is a duplicate?! –  May 25 '18 at 04:01
  • @RakeshKarandikar yes it is a duplicate and the accepted answer is not entirely correct. Everything in java is pass by value as pointed out in the answer in the link I provided. Read the answer in the old question carefully and you will realize that it is indeed a duplicate. – StaticBeagle May 25 '18 at 04:16
  • @StaticBeagle, could you please elaborate? The opening paragraph itself says _Unfortunately, they decided to call the location of an object a "reference". When we pass the value of an object, we are passing the reference to it. This is confusing to beginners._ - it is talking about objects. As per Mitchel0022's comments above, this is right; but variables of primitive data types are passed by value. Could you please confirm? –  May 25 '18 at 14:29
  • @RakeshKarandikar it creates a copy of the reference and passes that copy by value. Say you have `int[] array = ...` and you pass this array to a function `func(int[] arrayPrime)`, now inside the function `arrayPrime` points to the same location as `array`, thus `arrayPrime[2]` alters the value stored in `array[2]` but what happens if I do `arrayPrime = new int[]` inside the function? Now `arrayPrime` is no longer a reference to `array`. Think of a function passing a pointer `(int* p)`, C++ will pass a copy of than pointer to the function, thus passes the pointer by value. Clear as mud? =] – StaticBeagle May 25 '18 at 21:55
  • @StaticBeagle, absolutely clear. Thank you so much! :) –  May 26 '18 at 13:48

2 Answers2

0

I think you are just getting confused with the array part.

Array in java is a special object that contains references to other objects. You can very well change those "child" object's values as with any Object.

Remember this thumb rule that I keep to avoid confusion:

It doesn't matter what you do directly with the object but what you do inside it

Meaning that if you do some changes inside of any sub-object (or array item in your case) you'd see the side effects of it in your caller.

Shanu Gupta
  • 3,699
  • 2
  • 19
  • 29
-1

Primitive datatype variables will be passed by value. Object datatype variables will be passed by reference. Array is Object datatype, so it will be passed by reference.

  • This is incorrect. Please read the canonical duplicate link. It's a subtle but _very important_ distinction, because in Java there are only primitives and references. Everything is passed by value. – Jim Garrison May 25 '18 at 04:18
  • @JimGarrison, what about the array then? As Mitchell0022 pointed out in the comments above, I think they are passed by reference, because that explains the output and observation of the second code snippet. Could you please elaborate to help me understand? –  May 25 '18 at 14:30