0

During an interview I was asked to implement some linked list methods in Java. I've only had experience implementing linked list in C++ and there were somethings that concerned me. After having read this question and seeing the answer, I still have concerns.

class Link {
    public int data1;
    public double data2;
    public Link nextLink;

    //Link constructor
    public Link(int d1, double d2) {
        data1 = d1;
        data2 = d2;
    }

    //Print Link data
    public void printLink() {
        System.out.print("{" + data1 + ", " + data2 + "} ");
    }
}

My first problem is Java doesn't have pointers. I'm used to seeing something like Node* nextNode; instead of public Link nextLink;. How can a value be assigned to Link without pointers? Is it because in Java new returns an actual Link object? Or does it return a reference to it? If it returns a reference to it isn't that a problem since a reference is different than an actual Link object? Or in Java is any object's identifier actually a reference?

I know this is a matter of opinion, but why is it that linked lists seem to occur more in C++? I guess the language doesn't have built in support so a lot of tutorials written about linked lists are geared towards C++ (even if the focus was on the data structure).

13
  • In Java any object's identifier is actually a reference? I think that's the solution. Commented Apr 11, 2014 at 7:54
  • Yes to the last sentence in your long paragraph. In Java, any object's identifier is a reference. Commented Apr 11, 2014 at 7:54
  • Java objects references are actually pointers, having the value required to find the objects.i.e. address Commented Apr 11, 2014 at 7:54
  • You know an object reference in Java is a pointer, or why do we get a NullPointerException when accessing a null reference :-) Commented Apr 11, 2014 at 7:56
  • 1
    All object types in Java are reference types. The references are usually implemented with pointers, but they don't have to be. Does that clarify things? Commented Apr 11, 2014 at 8:17

4 Answers 4

1

It's cause java manipulates objects with their references. It's well known that java is pass by value, however the value of the objects is their addresses.

Sign up to request clarification or add additional context in comments.

11 Comments

So in C++ if you had just Node nextNode; instead of Node* nextNode everything would be much less efficient because copies are being passed, not pointers?
about that: is there a way around this? where i'm able to do something like foo(bar a){ a = new bar()} and a is changed outside the scope of bar too? because not being able to do this at all just seems like such a huge pain in the ass that I feel like there must be some way to do this
@Celeritas- I don't really remember the case in C++, however I think your answer is here.
@user2520938> What do you mean by this :" a is changed outside the scope of bar too"? Do you mean foo?
@Celeritas Probably the best way to find resolution to Java not having pointers is to look at your favorite edition of the Java: The Complete Reference series. Mine is the eighth edition, whereas here is the link to the ninth edition by Herbert Schildt: Java The Complete Reference 9/E [Paperback]. Pages 59 and 113 of the eight edition compares C/C++ pointers to Java references (not the same as declaring a Java instance that is assigned to the result of calling Java new on that object).
|
0

Java has its own LinkedList Class, if thats what you are asking for

check here:

http://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html

2 Comments

Also recall the Standard Library std::list container.
No, it doesn't look like the question is asking that at all.
0

In java, the objects are manipulated by their references, and a reference is similar to the pointer in C++, except that you won't be able to actually manipulate the addresses directly as you do in C++.

In java, when you use an Object, you use its reference, and hence the Link in your code is just a reference to the next Node object in the list.

Comments

0

The object references in java are just as good as pointers, the noticeable difference is that you cannot do pointer arithmetic here. MyClass myRef;in java just means a reference that will be pointing to some object. when you do MyClass myRef = new MyClass(); an object and created and its reference is stored in myRef. You should realize the difference between objects in memory and their reference in myRef. It is pretty much same as what we do in C/C++.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.