5

I have used a lot of C++ and I am quite confused with the way Java works : if I have a class

public class MyClass{
    private int[] myVariable;
     ...

    public int[] getVar(){
        return myVariable;
    }
}

and then I want to use my variable elsewhere :

public static void main(String[] args){
    MyClass myObject  = new MyClass();
    ...
    int[] temp = myObject.getvariable();
    // use of temp
    ...
}

is temp a copy or a reference of myVariable ?

How do you get a copy / a reference to it ?

Arcyno
  • 4,153
  • 3
  • 34
  • 52
  • so there is no way to save some memory and get a reference/pointer to `myVariable` ? – Arcyno Nov 04 '15 at 14:18
  • Java is always pass-by-value. Read http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value hence it is memory safe – AbuHayA Nov 04 '15 at 14:21

4 Answers4

6

There is only one int[] in your example. There is no copying at all. What is returned by the method getVar is a reference to the object.

After the line

int[] temp = myObject.getvariable();

both temp and myVariable are references to the same object. You can test this by doing e.g. temp[0] = 9;. The change will be visible in both places.

If you want to copy an array, you can use one of array.clone(), Arrays.copyOf(array, array.length) or System.arraycopy(...) but none of these are used in your example.

Paul Boddington
  • 37,127
  • 10
  • 65
  • 116
  • This answer seems right to me but it does not fit with the others : the modification of `temp[0]` will impact on `myVariable[0]` from my objet ? – Arcyno Nov 04 '15 at 14:26
3

That gets you confused because unlike C, Java is only pass-by-value. So you won't have a reference to that object but only a copy and then if you make any modification to that variable the original won't be affected. See this question for more details: Is Java "pass-by-reference" or "pass-by-value"?

EDIT

To be more specific, in your example you will actually get a reference to the array. But still, that's not like a C pointer. You get a reference only because the actual value you are copying it's a reference itself. As I said, you may find a more detailed explanation about this on the link above.

Community
  • 1
  • 1
Aurasphere
  • 3,841
  • 12
  • 44
  • 71
  • so there is no way to save some memory and get a reference/pointer to `myVariable` ? – Arcyno Nov 04 '15 at 14:20
  • @Arcyno You may want to take a look at this: http://stackoverflow.com/questions/1750106/how-can-i-use-pointers-in-java – Aurasphere Nov 04 '15 at 14:22
2

I can see what is confusing you, let me show you an exmaple.

MyClass.java

public class MyClass {
    private int i;
    private int[] a;

    public void setI(int i){
        this.i = i;
    }
    public void setA(int a[]){
        this.a = a;
    }

    public int getI(){
        return i;
    }
    public int[] getA(){
        return a;
    }
}

Test.java

public class Test {

    public static void main(String[] args) 
    {
        int a[] = {1,2};
        MyClass myClass = new MyClass();
        myClass.setI(5);
        myClass.setA(a);

        int i = myClass.getI();
        int b[] = myClass.getA();

        System.out.println(myClass.getI());
        System.out.println(myClass.getA()[0] + " " +myClass.getA()[1]);

        i = 10;
        b[0] = 3;
        b[1] = 4;

        System.out.println(myClass.getI());
        System.out.println(myClass.getA()[0] + " " +myClass.getA()[1]);
    }
}

When you see output of the above code you'll realize, primitive types like integer are always passed by value in Java. But when you pass arrays or other objects, like an object of Calendar class, it will be passed by reference.

Therefore changing value of array will be reflected at both the places while of an integer, wont.

This answer is acceptable to any suggestions. If anybody finds any inaccuracy, feel free to suggest edits.

Srujan Barai
  • 2,295
  • 4
  • 29
  • 52
1

There is an error in your class MyClass, you will have an compile error type mismatch: cannot convert from int[] to double. you have to change return type of your method to int[] like that:

public int[] getVar(){
return myVariable;

}

JAVA is always pass-by-value

Mourad BENKDOUR
  • 879
  • 2
  • 9
  • 11