473,473 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Linked List in Java

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?
Jul 22 '05 #1
7 1711

"Joe" <fi******@yahoo.com.tw> wrote in message
news:d8*************************@posting.google.co m...
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

Jul 22 '05 #2
Joe wrote:
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.

--
Regards,
Buster.
Jul 22 '05 #3
On 10 Oct 2004 21:33:51 -0700, fi******@yahoo.com.tw (Joe) wrote:
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
Jul 22 '05 #4
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.
Jul 22 '05 #5
"Joe" <fi******@yahoo.com.tw> wrote in message
news:d8*************************@posting.google.co m...
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"
--
Gary
Jul 22 '05 #6

"Joe" <fi******@yahoo.com.tw> wrote in message
news:d8*************************@posting.google.co m...
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
Jul 22 '05 #7
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.

Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: George M Jempty | last post by:
Is there a good linked list implementation in the public domain? Or that someone on this group has already written that they'd like to put in the public domain, right here on this newsgroup? I've...
3
by: Matt Williams | last post by:
I'm coming from a Java background - trying to build a linked list in C++. I've read up on and understand the differences btw. the two languages when it comes to pointers and most of the basics. ...
5
by: Shane | last post by:
Thanks in advance for the help. I'm new to the STL and havig a bit of trouble figuring out the whole iterator thing. I am using the <list> template and I can insert elements into the list just...
11
by: hana1 | last post by:
Hello guys, I just have a quick question about C++. I am moving from Java to C++. And as many of Java users know, all linked list and trees are already implemented and you just have to instantiate...
57
by: Xarky | last post by:
Hi, I am writing a linked list in the following way. struct list { struct list *next; char *mybuff; };
0
by: javi_moreno | last post by:
Hello, I am working with calls to remote methods in Jbuilder X with session beans. i have some problems marshaling a complex linked list. This linked list is a list of nodes. Every node has some...
10
by: AZRebelCowgirl73 | last post by:
This is what I have so far: My program! import java.util.*; import java.lang.*; import java.io.*; import ch06.lists.*; public class UIandDB {
0
by: huiling25 | last post by:
I have a Customer.java and a Transaction.java Transaction is when the user wants to deposit money. Customer.java includes the particulars of the user such as Name, AccountID, Balance etc. I have a...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.