1

I am testing this code.

class something {
    public static void main(String[] args) {
        int[] a = {1, 2, 3, 4, 5};
        sth(a);
        System.out.println(a);
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + " ");
        }
    }

    public static void sth(int[] a){
        int[] b = new int[a.length];
        for (int i = 0; i < a.length; i++) {
            b[i] = 0;
        }

        a = b;
        System.out.println(a);
        System.out.println(b);
    }
}

Expected output:

SAME MEMORY VALUE
0 0 0 0 0

What I'm Getting

[I@46d20791
[I@46d20791
[I@2c7f1f63
1 2 3 4 5

However changing the line

b[i] = 0; 

to

a[i] = 0;

reflects the change to

[I@46d20791
[I@46d20791
[I@2c7f1f63
0 0 0 0 0

Help me understand what is going on there?

Turing85
  • 16,432
  • 7
  • 31
  • 51
Buggy Coder
  • 373
  • 5
  • 16
  • 3
    Java is pass by value - the `a` in `main` and in `sth` are not the same - they just happen to initially point to the same object, until you reassign one of them - then they don't refer to the same object any longer... – assylias Apr 15 '15 at 20:52
  • @brso05 What you've said is exactly what I meant - maybe I was unclear... – assylias Apr 15 '15 at 21:02
  • I would say this is not an exact duplicate, as the question is how to change such a reference, where the answers in the duplicate question don't provide a solution or alternative to. (Answer here is to return the reference and set the passed reference to the result in the same line). – NESPowerGlove Apr 15 '15 at 21:09
  • other posts explains the confusion. Additional point, to achieve your expected result, you can do something: public static void sth(int[] a){ int[] b = a; <..> } – Ernusc Apr 15 '15 at 21:22

1 Answers1

0

In sth when you do a = b you are only changing the local reference a (the parameter) to point to b, you are not changing your main method variable a's reference.

In your main method, where you call System.out.print(a[i] + " ");, it's referencing the main method variable a (the only a in scope), which has been left unchanged.

When you change your code to a[i] = 0;, the local reference a, the parameter, is still referencing the main method variable a, so the code works as expected.

One way to have the behavior you want without changing much code would be to change your method to return the reference you want the method method a to use:

// in the method method
a = sth(a);
...

public static int[] sth(int[] a){
    ...
    return a; // if keeping local_a_reference=b;
}
NESPowerGlove
  • 5,456
  • 16
  • 27
  • How come `code' class something{ public static void main(String[] args){ int[] a = {1,2,3,4,5}; sth(a); System.out.println(a); for(int i = 0; i < a.length; i++){ System.out.print(a[i] + " "); } } public static void sth(int[] a){ for(int i = 0; i < a.length; i++){ a[i] = 0; } } } `code' would result in 0 0 0 0 as output? – Buggy Coder Apr 15 '15 at 22:23
  • @BuggyCoder Parameter `a` in `sth` in your comment version here is a reference to whatever is passed, not a copy. So it does modify main's `a'. In the example in your post you do `a = b`, which changes the local param `a` reference to reference some other array, but doesn't change main's `a'. Are you familiar with any other languages, perhaps I could clarify it by using examples in that language. – NESPowerGlove Apr 16 '15 at 04:52