-2

I'm new to Java and am confused by how "pass in by value" works.

int[] a;

public Constructor(int[] anArrayOfInt){ 
    a = anArrayOfInt;
}  

does variable a point to the object passed into the constructor? Do changes to variable a reflect in the object that is passed into the constructor?

1
  • "Do changes to variable a reflect in the object that is passed into the constructor?" - Should be easy to check that yourself Jun 25, 2016 at 5:02

1 Answer 1

2

Does variable a points to the object passed into the Constructor?

Answer is yes.

Does changes to variable a reflects in the object that is passed into the Constructor?

Answer is yes.

Because in java, parameter is passed by value. When you pass a object to method, actual, you are passing a reference of object.

2
  • this is where I'm a bit confused, if java pass in by value, shouldn't it create a copy of the passed in object (which is distinct to the original passed in object) and assign variable 'a' to the copied object? apology for asking a simple question like this, but i'm so confused about it
    – Thor
    Jun 25, 2016 at 5:07
  • 2
    @TonyStark No, it makes a copy of the reference, or in the case of primitives (int, long, boolean, etc) a copy of the value. Jun 25, 2016 at 5:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.