Linked List in Java

J

Joe

Hello, I don't know if it's appropriate to ask here.

But I am a C++ programmer, I think maybe you can more understand
what's my concerning.

I see the codes in Java to implement a linked list today.
I don't remember exactly what it is, just something like:

Class Node {
Element a;
Node next;
}
addNode(){
next = new Node;
}


What I know in C++, the line "Node next" is "Node* next".
I don't understand how this non-pointer writing could work. Isn't the
"next" object is allocate for a memory space when some instance of
class Node is created? Then there will be another node within the
next object, and another, and another....a infinit loop!

I don't understand.
Can anyone explain this?
 
J

John Harrison

Joe said:
Hello, I don't know if it's appropriate to ask here.

But I am a C++ programmer, I think maybe you can more understand
what's my concerning.

I see the codes in Java to implement a linked list today.
I don't remember exactly what it is, just something like:

Class Node {
Element a;
Node next;
}
addNode(){
next = new Node;
}


What I know in C++, the line "Node next" is "Node* next".
I don't understand how this non-pointer writing could work. Isn't the
"next" object is allocate for a memory space when some instance of
class Node is created? Then there will be another node within the
next object, and another, and another....a infinit loop!

I don't understand.
Can anyone explain this?

No, a pointer is a pointer. It does not allocate any memory except for the
pointer itself. In C++ and Java you have to allocate memory for the object
pointed to explicitly, it doesn't happen automatically.

If you write this

class Node {
Element a;
Node next;
};

That would be an infinite loop in C++ (and therefore illegal), but in Java
everything is a pointer (except stuff like int and double).

john
 
B

Buster

Joe said:
Hello, I don't know if it's appropriate to ask here.

Not really.
But I am a C++ programmer, I think maybe you can more understand
what's my concerning.

I see the codes in Java to implement a linked list today.
I don't remember exactly what it is, just something like:

Class Node {
Element a;
Node next;
}
addNode(){
next = new Node;
}


What I know in C++, the line "Node next" is "Node* next".

That's about right.
I don't understand how this non-pointer writing could work.

The string of symbols "Node next" has a different meaning in Java than
in C++. (That's a consequence of what you just said.)
Isn't the
"next" object is allocate for a memory space when some instance of
class Node is created?

No it isn't is. "Node next" only declares a reference, like "Node *
next" in C++, as you said. (A pointer is a kind of reference, in the
sense of being something which refers to something.)
Then there will be another node within the
next object, and another, and another....a infinit loop!

No loop.
I don't understand.
Can anyone explain this?

You didn't understandd because you had drawn an unwarranted conclusion
from a formal similarity between syntax from two different languages.
 
T

Tom Widmer

Hello, I don't know if it's appropriate to ask here.

But I am a C++ programmer, I think maybe you can more understand
what's my concerning.

I see the codes in Java to implement a linked list today.
I don't remember exactly what it is, just something like:

Class Node {
Element a;
Node next;
}
addNode(){
next = new Node;
}


What I know in C++, the line "Node next" is "Node* next".
I don't understand how this non-pointer writing could work. Isn't the
"next" object is allocate for a memory space when some instance of
class Node is created? Then there will be another node within the
next object, and another, and another....a infinit loop!

I don't understand.
Can anyone explain this?

Java references (like "a" and "next" above) are semantically very
similar to C++ pointers, except that you can't do pointer arithmetic,
peculiar casts, etc. Java only has a handful of types that behave like
C++ value types - they are the built in types like int, and the
reference types.

Tom
 
J

Joe

Thanks guys,

I understand now.
Except for the built-in type(i.e. int), all other type objects will
not allocate any memory (like the pointer in C++) until "new"
statement is applied to it.
 
G

Gary Labowitz

Joe said:
Thanks guys,

I understand now.
Except for the built-in type(i.e. int), all other type objects will
not allocate any memory (like the pointer in C++) until "new"
statement is applied to it.

No. Normal declaration/definitions will allocate memory for any type.
Example:

string myString = "My name";

This allocates the string object initialized to "My name"
 
J

John Harrison

Joe said:
Thanks guys,

I understand now.
Except for the built-in type(i.e. int), all other type objects will
not allocate any memory (like the pointer in C++) until "new"
statement is applied to it.

Not quite. Remember that the pointer and the object pointed to are separate
things. When you declare a pointer you get memory for the pointer itself
(just like int). You do not get memory for the object pointed to, you have
to allocate that yourself.

john
 
B

BAMY

Hi, there!

I'm am from C++ world too.
So as I understand if You have an "inner class" or nested class in Java,
there is no called constructor for the inner class when the nested object
is declared. Isn't so?
Because otherwise there will a recursive class with infinitive loop. No
calling constructor from contructor - that is the answer, and that is like
a pointer?
Thank You.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,183
Messages
2,570,967
Members
47,520
Latest member
KrisMacono

Latest Threads

Top