How can I delete an object passed into a Java method?

我是研究僧i 提交于 2019-12-13 04:28:28

问题


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?


回答1:


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().




回答2:


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.




回答3:


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.




回答4:


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.




回答5:


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.




回答6:


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.

}



回答7:


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.




回答8:


Try:

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

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


来源:https://stackoverflow.com/questions/12591294/how-can-i-delete-an-object-passed-into-a-java-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!