0

So my current code is:

int x = 5;

int[] one = new int[1];
one[0] = x;     
int[] two = new int[1];
two[0] = x;

x = 10;

System.out.println(one[0]);
System.out.println(two[0]);

The aim here is to get an output of two 10's. Instead what I get is two 5's being printed.

I know that in C++ there is a way of saying &x to refer to a reference type however I don't know of anything similar in Java?

I'd be really grateful if someone could help me out here.

EDIT

Cheers guys. I ended up making my own class and using that instead.

class Ideone { 

    public static void main (String[] args) throws java.lang.Exception 
    { 
        MyTester x = new MyTester();
        x.i = 5;
        MyTester[] one = new MyTester[1];
        one[0] = x;
        MyTester[] two = new MyTester[1];
        two[0] = x;

        x.i = 10;

        System.out.println(one[0].i);
        System.out.println(two[0].i); 
    } 
} 

class MyTester 
{ 
    public MyTester() {} 
    public int i;   
}
Callum
  • 50
  • 7
  • Primitive values are copied. Try using an Integer array. – Scot Nov 26 '14 at 18:57
  • 2
    @Scot Integers are immutable, so assigning new *value* to `Integer` will not be seen in array. What OP needs is change *state* of `x`. – Pshemo Nov 26 '14 at 18:59
  • 1
    @Callum you could create your own Integer class implementation with a setter ie. mutable then you could accomplish this... – brso05 Nov 26 '14 at 19:00

2 Answers2

0

Nope, no can do. The closest thing you could do is say one = two, and then one[0] = 10, and then two would point to the same array as one and would reflect the change.

You can't do anything like that with just the bare primitive, though.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • Would it be possible to do so with one of my own classes? The idea in the end (this was just an example) was to have one object which can be used in multiple arrays so it's fields update across them. – Callum Nov 26 '14 at 18:48
  • if you put MyObject type objects in the array, and then do myObject.myint = x; then yes it would work. – MeBigFatGuy Nov 26 '14 at 18:50
  • Objects are passed as references; references to the same object, whether in an array or a local variable, will reflect changes made via other references. `myReference = a` will not change other places, but `myReference.foo = bar` will change other references to the same object. – Louis Wasserman Nov 26 '14 at 18:51
  • @Callum Yes, if you store same instance in your arrays and you will change *state* of this instance this change will be visible everywhere. – Pshemo Nov 26 '14 at 18:52
  • Ah awesome, cheers mate. That worked I used the code: `class Ideone { public static void main (String[] args) throws java.lang.Exception { MyTester x = new MyTester(); x.i = 5; MyTester[] one = new MyTester[1]; one[0] = x; MyTester[] two = new MyTester[1]; two[0] = x; x.i = 10; System.out.println(one[0].i); System.out.println(two[0].i); } } class MyTester { public MyTester() {} public int i; } ` – Callum Nov 26 '14 at 18:55
  • @shekharsuman: It doesn't make a difference; once they point to the same reference that's enough. – Louis Wasserman Nov 26 '14 at 19:01
  • 1
    @shekharsuman: I'm not sure what clarification you're expecting. Whether you write `one = two` or `two = one`, if you do one of those first and then say `one[0] = 10`, that change will be reflected by `two[0]`. – Louis Wasserman Nov 26 '14 at 19:03
0

It doesn't work as you expected since it's called by value. There is a nice old article with many good examples to read: Is Java "pass-by-reference" or "pass-by-value"?

Community
  • 1
  • 1
Paul Lo
  • 6,032
  • 6
  • 31
  • 36