Possible Duplicate:
Java, pass-by-value, reference variables
I am a bit confused on how exactly JAVA pass by value works with object. For e.g. if I pass a object as a parameter to the method. I understand that its address is passed as value. Ok, is a duplicate of the object is kept at the original place form where the object is passed, because if I create a new reference to the object in the called API and change something in it, it doesn't get reflected in my caller API.
Below is a typical piece of code where I try to delete a tree but it's still there.
public class DeleteTree {
public static void main(String[] args) {
Node root = new Node(5);
for(int i = 0 ; i < 10 ; i++){
if(i == 5) continue;
root.insertNode(i);
}
deleteTreeNonRecursive(root);
System.out.println(root.key);
}
public static void deleteTreeNonRecursive(Node root){
Queue<Node> q = new LinkedList<Node>();
q.add(root);
while(!q.isEmpty()){
Node temp = q.poll();
if(temp.leftChild != null)q.add(temp.leftChild);
if(temp.rightChild != null)q.add(temp.rightChild);
temp = null;
}
}
Expected O/P: nullpointer exception.
Actual O/P: 5.