Why doesn't Java call by reference?

When I pass root.left from add to add(data, parent), I'm not passing the reference to this object so that it gets changed, but only a kind of copy that has no reference to my root. How do I work around this? A beginner's programming language should be able to do this.

 public class Tree {    TNode root;    Tree(){        this.root = null;    }    public void add(int data){        if(root == null){            root = new TNode(data);        }else{            if(data < this.root.data){                add(data, root.left);                System.out.println("miosty");            }else {                add(data, root.right);            }        }    }     public void add(int data, TNode parent){        if(parent == null){            System.out.println("Zu");           parent = new TNode(data);        }else{            if(data < parent.data){                add(data, parent.left);            } else {                add(data, parent.right);            }        }    } }
(2 votes)
Loading...

Similar Posts

Subscribe
Notify of
9 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
KarlRanseierIII
1 year ago

Java makes calll by reference, but your design is also a little cumbersome and ‘nasty’

    public void add(int data){
        add(int data, root)
    }
    public void add(int data, TNode current){
       if (current==null){
          current=new TNode (data);
          return;
       }
       // Gleichheit sollte getrentn behandelt werden.
       if (data < current.data) add (data, current.left);
       else add (data, current.right);
       return;
   }

This should look more structurally, with recursive work being done here, but you can also implement iteratively quite similar.

P.S.: You may notice in the course of further implementation that the descent takes place as well as in the search, so that you may that adds to the use of the search.

EnderLuca
1 year ago

You have to imagine that you have your TNode somewhere in the store and your program has a reference to this object. If you now pass the reference as an argument in a method, a new Reference creates but shows the same object.

This means that if you create a new object and show your second reference, then nothing changes to your original object, and your first reference still shows it.

This means the code in the else block is fine at the second overcharging of “add()” but not the assignment “patent = new TNode(data) ;” .

EnderLuca
1 year ago
Reply to  DavidGoggins

In C it would only work if you used a double pointer, e.g. “int **”. In Java it is not possible to fit a reference by reference.

However, another user wrote to you about 3 hours ago, in an answer to the same question.

triopasi
1 year ago

Yes, you give a reference. If parent.left is NULL, you will start a new node that will be deleted directly. Apart from this, the methods are copy&paste and therefore nixht well written.

triopasi
1 year ago
Reply to  DavidGoggins

Because there are no references to the object. It only exists within the function. Then it will be deleted when the function is finished.