-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?

OneCricketeer
  • 126,858
  • 14
  • 92
  • 185
Thor
  • 8,608
  • 10
  • 43
  • 113
  • "*Do changes to variable a reflect in the object that is passed into the constructor?*" - Should be easy to check that yourself – OneCricketeer Jun 25 '16 at 05:02

1 Answers1

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.

OneCricketeer
  • 126,858
  • 14
  • 92
  • 185
ThiepLV
  • 1,089
  • 3
  • 9
  • 21
  • 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 '16 at 05: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. – Jim Garrison Jun 25 '16 at 05:38