Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pointers in java ???

Status
Not open for further replies.

mu4life66

Programmer
Nov 17, 2003
5
0
0
US
Hi all!!
i'm new to java and i got a simple question.
how does pointers work in java?
let say i want to creat a link list...with nodes...in C++, it'll look like this:

class Node
{
public:

// constructors
Node():
next(this),prev(this){};

Node(const ItemType data):
item(data),next(NULL),prev(NULL){};

// public data members
ItemType item;
Node* next;
Node* prev;
};

does java have * ? i know java has ArrayList class and stuff like that, but what if i want to creat one from scratch?
 
A very lousy answer would be:
--- start here ---
There are no pointers.

Replace Foo* foo with Foo foo.
Replace foo->bar (); with foo.bar ();
and foo->a = 7; with with foo.a = 7;

Not absolutely sure, but I guess you would translate it to this:
Code:
class Node
{
	public ItemType item;
	public Node next, prev;

	public Node ()
	{
		next = this;
		prev = this;
	}

	public Node (final ItemType data)
	{
		item = data;
		next = new Node ();
		prev = new Node ();
	}
}
--- end lousy ----

In Detail, things get more complicated.
Objects are passed as methodarguments as 'copy of reference' (so changing the reference will not affect the caller - while changing the referenced values will).



seeking a job as java-programmer in Berlin:
 
wow.......

thanx stefanwagner

i guess java just want to make everything really simple.

I really don't understand how java work with out letting user mess with the pointers... I try to find out more about this in many java books, but no luck so far....they don't even mention pointers at all....

do you know any reference i can check out?

thanx
 
I've only really done a little C programming, but apart from pointer arithmetic for manipulating arrays ... why would you want pointers in Java ? One of the main tenets of Java is to try to take away the nightmare of C style memory leaks. Ie, when you declare an object or primitive, you don't "really" have base acccess to the pointer ... the JVM takes care of it via object references in the heap, and clears the pointers for each object for you, as appropriate.

I think C programmers get a bit hung up on the whole pointer thing when trying to use Java. Just be sensible, write neat code, and cleat you objects up. If you leave refrenced objects around on the heap, then you will get dangling pointers & memory leaks etc.

Maybe I'm missing something ?!!


--------------------------------------------------
Free Database Connection Pooling Software
 
Hello all.
Java does not support pointers at all.
I came up from C to C++ to Java and now into C#.
It took a while getting used to not being able to manipulate pointers in Java.
That is why Java is at this point, slower than C or C++.
If you want to manipulate variable values in Java like you do in C++ (through pointers), you have to write functions in the originating Class that the variable value is assigned in.
Good luck with it.
-D.

Misce stultitiam consiliis brevem...
 
The reason for no pointers in Java (as it was explained to me) is that Java's two main principles are:

1) Portability
2) Ease of maintenance

Using pointers would violate #1, as pointers are very machine-architecture specific. The 6502 uses a 16-bit pointer and is a little-endian machine, the PowerPC uses a 64-bit pointer and is an either-endian processor (the OS chooses which to use).

If your java program is to run on both of them, it can't use pointers (the architectures are just too different). The JVM isolates your program from these differences (it is platform-specific).

Chip H.



____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top