1

I know that in Java, besides several primitive types, everything is a reference type (of an Object). However, I do not find a simple way to do operations that are usually done by using references in C++. In fact, I think a reference in Java is a bit like a pointer in C++, in that if I assign a variable to another object, the previous object referred by this variable will not be affected. However, in C++, if I have an int a and I make int& b the reference of "a", then these two are actually the same thing - the assignment of "b" will change the value of "a".

The reason for my question is that I am writing a BST ADT and I encounter some trouble, which I would solve using references in C++. For example, I want to implement the add method:

void add(K key, V val) {
    if (this.key.equals(key))
        this.val = val;
    node child = next(key);  
    // next is a function to determine the child in the next layer according to the key
    if (child != null)
        child.add(key, val);
    else {
        ...  // FIX THIS
    }
}

Now, if I am writing C++, I can let next return a reference and child be node& so that I only need to let child = new node(key, val); and I am done. But here in Java the same statement will only assign child to another node and the tree will not be affected.

JamesTZ
  • 25
  • 4
  • 1
    What Java calls a reference, C++ calls a pointer. (NPE is vestigial of that correlation.) Java does not have C++ references. Java parameters are only pass-by-value (primitive or pointer). – Eljay Jan 17 '20 at 13:43
  • Thanks for your reply. I know that Java references are basically pointers in C++. So I do not need try to find the C++ references equivalent in Java. You have answered my question. – JamesTZ Jan 17 '20 at 14:02

1 Answers1

1

There is no way to make an int variable 'point at' the same memory location as another int variable in java. Completely impossible.

There are, however, custom types, such as AtomicInteger that can do the job:

AtomicInteger x = new AtomicInteger();
x.set(10);
m(x);
System.out.println(x); // prints 20

...

void m(AtomicInteger x) {
    x.set(20);
}

This doesn't help you 'adapt existing APIs', but that's the point. If I write a method that receives an int parameter, I know that nothing could possibly mess with it during my method's execution. That's nice to have, and java intentionally does not allow you to break this without explicit consent (which takes the form of having the param be of type AtomicInteger instead).

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • Thanks for your answer. I am rather new to Java, and I find without being able to make references a little inconvenient in comparison to C++. – JamesTZ Jan 17 '20 at 14:51
  • 1
    @JamesTZ I would strongly recommend that you don't try to code in Java like you would in C++, but instead learn how to program in Java. You will need to address problems differently, otherwise programming in Java will be a painful experience (and developers well-versed in Java will probably not like to read or touch your code if you try to program C++ in Java). – Mark Rotteveel Jan 17 '20 at 15:27