NSF
NSF

Reputation: 2549

Java reference does not work as a pointer?

Here is a piece of code in my algorithm:

public void insert(int element) {
    _insert(element, root);
    System.out.println(root);
}

private void _insert (int element, Node t) {
    if (t == null) {
        t = new Node(null, element);;
        return;
    }
}

Node here is a predefined class.

When the public method insert is called, the private method will be called and check if the tree is empty. If so, a new node would be created at root position.

The output is supposed to be a node. However the actual output is null which means root is not updated though it's passed as t into the private method.

This should work under C++ with pointers. Maybe I misunderstand something in Java?

Upvotes: 2

Views: 1803

Answers (2)

SylvainL
SylvainL

Reputation: 3938

In Java, references are a mix of C++ pointers and references. Sometimes, they behave like a C++ pointer and some other times, like a C++ reference. When making an assignation, passed as an argument to a function or used in an equality test, they behave like pointers but when accessing a member, they behave like a C++ reference. That's it, writing t.x in Java is always interpreted like the equivalent of t->x for a pointer in C/C++ (or t.x for a C++ reference but unlike Java, a C++ reference never behave like a pointer).

Futhermore, in Java and in general object oriented programming, most people will not consider to represent an empty list with a null variable as to be a good idea. Once initialised, a variable should always reference a fully functional object and therefore, an empty list should be identified by the internal state of the object; not by the use of a null reference. Using the return value of a function to re-assign a node that is itself passed as an argument to the same function is more like some good old fashioned C/C++ hacking then proper modern object oriented design. Therefore, instead of:

root = _insert(root, element);

you should write something like:

MyList root = new MyList();
root._insert (element);

and somewhere in your class MyList:

private void _insert (int element) { 
  if (this.endNode == null) { 
      this.endNode = new Node(); 
  } else {
      this.endNode.nextNode = new Node();
      this.endNote = this.endNote.nextNode;
  }

  this.endNode.element = element;
}

If you want your _insert function to be able to insert an element at an arbitrary position inside the linked list, then you should past the parent of the node (or null for your element to be inserted in the very first position) or use a double linked list.

BTW, Java already has a full set of collections (both generic and non-generic) to represent trees and lists that should be able to covert must basic needs.

Upvotes: -1

Mark Byers
Mark Byers

Reputation: 837936

Java always uses pass-by-value for method calls. When you pass a reference type to a method, a copy of the reference is made.

The following line changes t to refer to a new object, but it does not change the original variable root:

t = new Node(null, element);

You can change your method to return the inserted node, as follows:

private Node _insert (int element, Node t) {
    if (t == null) {
        t = new Node(null, element);
    }
    return t;
}

And call like this:

root = _insert(element, root);

Upvotes: 6

Related Questions