0

I know that Java passes objects by reference, so when an object is passed as an argument into a method, anything that is done locally to the argument inside the method is done to the actual object.

void main(){
 String[] array1 = {"a","b","c"};
 someMethod(array1);
 print(array1.length);
}

void someMethod(String[] array){
 /..code here../
 array = null;
}

I would expect to get a null pointer exception when trying to print array1.length because my method set it to null. However this is not happening. Is there a reason for this?

jII
  • 1,134
  • 2
  • 17
  • 29
  • 5
    Java parameters are passed by *value* always. The value may be to a reference variable but still it is always pass by value. – Hovercraft Full Of Eels Sep 25 '12 at 21:30
  • java passes values of reference, so this value points to reference. Once we change the object, that object will be changed. But if we reinitialize it inside mwethod, the real object will not be changed – Elbek Sep 25 '12 at 21:50

8 Answers8

3

Objects are passed by reference value (pass by value) not just reference.

When you set null to array in someMethod(), it technically sets the array which is scope someMethod to null not the one in main.

If you would like to null the outer reference also, then just return null instead of assigning null.

I would suggest read this thread to understand this concept properly.

Community
  • 1
  • 1
kosa
  • 65,990
  • 13
  • 130
  • 167
  • You should use concrete links when talking about a specific answer instead of referring to it by position. You can get the permalink via the "share" link under the answer. Who knows which answer will have the most votes 1 or 2 years from now? ;) ([Concrete link to referenced answer](http://stackoverflow.com/a/73021/646634)) – Brian Sep 25 '12 at 21:38
  • @Brian: That is valid point. Updated link as you suggested. Next time I will make sure. – kosa Sep 25 '12 at 21:39
2

array within someMethod is just another pointer to the object. So when you're setting array = null;, you're just setting the pointer to null. The object still exists on the heap, and is still pointed to by array1 in main().

Tim S
  • 5,023
  • 1
  • 34
  • 34
  • Many great answers, but this was the most concise, and helped me understand how this is different to c++. – jII Sep 26 '12 at 04:54
2

The array reference is passed by value. So your method gets a copy of the reference to the array. Using this reference, you can change the contents of the array, but if you change the reference itself - i.e. you try to set the parameter to null, or to a new array - the changes won't be reflected outside the method.

pb2q
  • 58,613
  • 19
  • 146
  • 147
2

When you call someMethod with argument array1, the method is passed a reference to the array that array1 points to. Essentially there are now two references to the same object - one in the main scope and one in the someMethod scope. If you change array in someMethod to point to null, this has no effect on the original reference.

andersschuller
  • 13,509
  • 2
  • 42
  • 33
2

You must be coming from C++ :)

By "reference" we actually mean the value of the reference. When an object is passed to a method, its reference is copied to the stack, so when you set array = null, you're actually setting the stack-local array to null, not the actual reference from the calling method.

To do something like what you want:

void main(){
    String[] array1 = new String[] {"a","b","c"};
    array1 = someMethod(array1);
    print(array1.length); // NullPointerException thrown here
}

String[] someMethod(String[] array){
    /..code here../
    return null;
}

Java will automatically reclaim the space used by your array when it starts to run out of space and runs its garbage collector.

Think of it as being similar to passing a copy of the pointer instead of the pointer itself. Manipulating the copied pointer won't change the original, as you've observed in your example.

Brian
  • 17,079
  • 6
  • 43
  • 66
2

Java passes values of reference, so this value points to reference. Once we change the object, that object will be changed. But If you reinitialize it inside method then the real object will not be changed; Here is the example:

void main(){
    String[] array1 = new String[] {"a","b","c"};
    someMethod(array1);
    print(array1[0]); //you will see q
}

void someMethod(String[] array){
    array[0] = "q"; //i am NOT overriding it, I am changing the existing object

}

here is i will reinitialize it

void main(){
    String[] array1 = new String[] {"a","b","c"};
    someMethod(array1);
    print(array1.length); //you will see 3
}

void someMethod(String[] array){
    array = null; //I am totally overriding the object, so it will not affect to the object inside main method.

}
Elbek
  • 3,434
  • 6
  • 37
  • 49
1

You are right - anything you change in object will retain, but you are not changing anything in referenced object but the reference which itself is a value.

Krizz
  • 11,362
  • 1
  • 30
  • 43
1

Try:

void main(){
 String[] array1 = {"a","b","c"};
 someMethod(array1);
 print(array1[0].length());
}

void someMethod(String[] array){
 /..code here../
 array[0] = null;
}
Hot Licks
  • 47,103
  • 17
  • 93
  • 151