0

This is a pretty simple question but I'm new to java.

The linked list found in java.util.LinkedList.

I saw elsewhere that Java does not use pointers...

When I create a new list and add new elements to it, does the list contain references to the data and to the next/previous elements or does it also contain the object itself.

  • 2
    stackoverflow.com/questions/1750106/… may be a useful SO answer about Java references and pointer concepts. – JB King Mar 20 '14 at 3:29
  • Do you mean if you change the object in an entry of a LinkedList it changes the original? If so then yes. It also applies to the next and previous, you can get the next object in the list and perform operations on it also. – OKAN Mar 20 '14 at 23:38
  • 2
    Java uses pointer internally -- they are call references. Mostly you work with references to an Object "Oa = Ob" just sets Oa to point to the same object as Ob. This difference between Java and C is that you can only assign references you cannot manipulate them. – James Anderson Apr 2 '14 at 1:10
3

In Java you have primitives (int, long, etc.) and objects. Objects are references (pointers) to complex data.

Java always passes values; so within your list you will have actual values of what you put in. BUT: Since objects are references to data, the values passed will be the references, not the data itself.

Pragmatic result: You put object A in a list and change its data somewhere in your code. Object A will still be in your list (you didn't change A but its data) but the A in your list will reflect the changes you've made somewhere else.

Car a = new Car();
List myList = new LinkedList();
myList.add(a);
a.driveHome();
Car carFromList = myList.getFirst();
carFromList.isDrivenHome(); // true
  • a reference is NOT a pointer. And no, Java does NOT pass values for object instances, it passes references to object instances BY value. – jwenting Apr 2 '14 at 8:40
  • @jwenting Right, a reference is not a pointer but for the purpose of understanding the matter at hand the difference seemed irrelevant to me. Would you differ? And regarding the passing of references to objects: Isn't that what I wrote? How did you understand my answer? – jhr Apr 2 '14 at 8:50
  • 2
    you say Java passes values. THAT is wrong. Java passes a reference to an Object instance by value, NOT the Object instance itself as you're claiming. – jwenting Apr 7 '14 at 16:17
  • It's interesting. I'm pretty sure we mean the same thing, yet you seem not to be reading what I meant to write. In any case, yes, Java passes references to objects (never the object data). In case of non-objects, i.e. primitives, Java does pass values. What I meant to describe was: Java always passes values (it passes by value) - primitives as well as objects - but an object is just a reference to complex data, so what's passed (by values) in case of objects if just their reference. Anywho, I guess the topic is pretty clear by now. :) – jhr Apr 7 '14 at 16:26
  • @jhr I know it is more a matter of vocabulary but I don't agree with the sentence Since objects are references to data, the values passed will be the references. The reference is not what we call the object. – C.Champagne May 7 '14 at 15:44
1

You can check out the OpenJDK LinkedList implementation on GrepCode. The LinkedList class itself contains a reference to an Entry named header, which contains references to two more Entry objects for the next and previous elements in the list.

0

To answer specifically: note that java.util.LinkedList defines a doubly-linked list.

The list has two member variables first and last that reference ("point at") the first and last nodes. List nodes are of type Entry<T>, where T is the type of the objects you are inserting.

Each list node or Entry thus contains:
T data : the actual item (object) inserted
Entry<T> next : a reference to ("pointing at") the next node (null if last)
Entry<T> previous : a reference to ("pointing at") the previous node (null if first)

For example, list (A,B,C,D) is represented as follows:

/   \  ->  /   \  ->  /   \  ->  /   \
| A |      | B |      | C |      | D |
\   /  <-  \   /  <-  \   /  <-  \   /
  ^                                ^
first                            last
0
  • LinkedList uses index to store elements
  • Elements are point(linked) to each other

There are 3 types of linked list

1.singly linked list = each node point the next node. (x)--->(y)--->(z)--->nothing

2.doubly linked list = each node point to next and previous node . (x)<-->(y)<-->(z)

3.circularly linked list. = each node point to next node (like singly-linked lists) last node and fist node linked (x)--->(y)--->(z)--->(x)

  • 1
    The question asks about a specific case: the internal structure of a java.util.LinkedList. Your answer describes general theory on LLs. Can you edit your answer to target the specific scenario in question? – metacubed Aug 6 '14 at 6:38
  • sammy_123 ask the question "When I create a new list and add new elements to it, does the list contain references to the data and to the next/previous elements or does it also contain the object itself." Obviously list contain references. In My answer I have clearly mention it. – lasa Aug 6 '14 at 6:49
  • yes.. but the question is specifically: "Java (allegedly) does not use pointers, so how does the Java LL store nodes and references?" Hope you realize what I'm driving at. – metacubed Aug 6 '14 at 6:55
  • Yes I got the point here problem is term I have used is incorrect Thanks any way. – lasa Aug 6 '14 at 7:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.