Archived

This topic is now archived and is closed to further replies.

[java] pointers in java

This topic is 5399 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

sorry, I''m new to java. I''m fairly proficient with c++, and I was wondering if there is an equavalent type of memory access such as pointers in java as in c/c++. I know you can create an array with int[] something. then calling new int=..., but how would a linked list or something be implimented? thanks for your time, Joe My Homepage

Share this post


Link to post
Share on other sites
Although pointers don''t exist in Java, references do.
What are references?

When you do this:
Object a;
you create a reference to an Object. There is no actual Object there yet, but it''s ready to point to one.

Then you do this:
a = new Object();

Suddenly, a "points to" an Object.

So, how to implement a linked list? I''m still learning about it myself, but an idea is to create an inner class called Link with two public fields: Object data, Link next. Since an instance of any class can be pointed to by an Object reference, you''re fine (just remember to cast to your more specific class).

Then, another inner class that implements Java''s ListIterator interface.

Oh, and as for the actual LinkedList class itself? A singly-linked list only needs a reference for the first element. So a field like Link first will work perfectly. Manipulating the list is really just changing where the references point to.

Example, an addFirst(Object e) method:
Link newLink = new Link();
newLink.data = e;
newLink.next = first;
first = newLink;

and you''re done! You create the link, then set its data to the object passed. Then you say that newLink.next should be the first link in the list at the moment. Then newLink becomes the new first link.

Of course, this is more from a specific implementation in my textbook. There are probably many other ways to do it in Java. In addition, I might have made some stupid mistakes somewhere. Hopefully, this all illustrates the basic point.


This isn''''t life in the fast lane, it''''s life in the oncoming traffic.
-- Terry Pratchett

Share this post


Link to post
Share on other sites
I dont know if this will work but give it a shot.

Linked list of int's

  
public class node
{
public int data;
public node next;
}
public class LinkedList
{
private node head;

public void addNode(node newNode)
{
if(head==null)
{head = newNode;}
else
{
node currentNode = head;

while(currentNode.next!=null)
{currentNode = currentNode.next;}

currentNode.next = newNode;
}
}

public void removeNode...
...
...
...
}


I just came up with this off the top of my head, so it might not work, but i dont see why it wouldn't (cept maybe syntax errors ). Anyways it is essentially the same as in c++, just remember that the variables are references.

[edited by - IllMind on March 2, 2003 4:46:42 AM]

Share this post


Link to post
Share on other sites

It''s also worth noting that while objects are passed by reference, the built-in numeric and boolean types (char, int, double, etc) are passed by value.

Java also has many data structures implemented for you in the util package. But these structures don''t take primitive types, so you''ll have to convert them to their related object types (Character, Integer, etc) before you add them to the LinkedList and after you get them out of the list before you can do anything useful with them.

Hope this helps.

/B

Share this post


Link to post
Share on other sites
quote:

It''s also worth noting that while objects are passed by reference, the built-in numeric and boolean types (char, int, double, etc) are passed by value.



I think that''s a common misconception. Everything in Java is passed by value, AFAIK. Objects just seem like they are being passed by reference because the Object variable stores a memory address. That memory address is passed to the parameter. Try to make a method that takes an Object reference as a parameter and makes it point to something else. It won''t work because the memory addresses are copied by value.



This isn''''t life in the fast lane, it''''s life in the oncoming traffic.
-- Terry Pratchett

Share this post


Link to post
Share on other sites
Kentaro - of course not, this is correct behaviour, because in order for the parameter reference to be changed, it would have to have a reference to a reference.

Kind of like in C:


int g = 1234;

void do_ref( int* c ) {
c = &g;
}

void do_ref_ref( int** c ) {
*c = &g;
}

void main( void ) {
int zero = 0;
int* ptr = &zero;

printf( "Value of *ptr is %d\n", *ptr );

do_ref( ptr );

printf( "Value of *ptr after do_ref is %d\n", *ptr );

do_ref_ref( &ptr );

printf( "Value of *ptr after do_ref_ref is %d\n", *ptr );
}



In Java - memory addresses are copied by value - that is, references are copied by value. Objects aren''t.

AFAIK :D

Share this post


Link to post
Share on other sites
Form a C++ programmers perspective, there''s no references in Java. Java references are allowed to be Null, so they are semantically equivalent to C++ pointers. Syntactically, you just always use . to dereference them (again in C++ terms you would always use -> since objects are not allowed to be made on the stack).

quote:
Kentaro
I think that''s a common misconception. Everything in Java is passed by value, AFAIK. Objects just seem like they are being passed by reference because the Object variable stores a memory address.

So in other words, Object are always passed by address . The difference between pass-by-address and pass-by-reference is largely syntactic - the semantics are the same (and the semantics are what really matter). In some languages, referneces are not suppose to be null, so the semantics are slightly different - in Java they''re the same.

Share this post


Link to post
Share on other sites
No, objects aren''t passed at all in java. Object pointers are passed by value, just like primitives.

C and C++ allow you to create instances of structs/classes on the stack, and access them directly, without using a pointer. In java, instances of classes can only be created on the heap, and so can only be accessed via a pointer.

Talking about passing objects in java is a very lazy and imprecise use of terminology. It usually results in confusing people who are just learning the language, and so should be avoided when teaching.

Just teach people what actually happens: Everything in java is passed by value. Just be very clear what the value actually is for reference types.

Share this post


Link to post
Share on other sites
at ease ... it is ok (although you could add a tail-node reference for quicker adding ) ...
But why not use the collections hierarchy, that's already in the standard distribution ?
I personally am so lazy, that I would use every line of code I can, instead of mindlessly re-[implement|invent]-ing the wheel.

[edited by - Petr Stedry on March 4, 2003 2:06:49 AM]

Share this post


Link to post
Share on other sites
heh, thanks petr, I wasnt trying to get too fancy, i was just testing my memory to see how i would fare in a job interview if they would ask that type of question

Share this post


Link to post
Share on other sites
Well,

you would wonder what they ask sometimes

When I applied at a large company, their main concern was:
"And you could relocate to XY ?"
and I said:
"No problem."
my skills did not matter much then (they just asked a few simple questions and let me fill a stupid enough test consisting of 20 questions, that took me some 15-20 minutes to complete).

So this may happen to you too. Or again it may not. The people in our country aren''t willing to relocate because of work (compared to the US).

Am I wrong or am I wrong?

Share this post


Link to post
Share on other sites
You might take note of the fact that you''re trying to access a private member variable, next, of some other object inside your addNode method.

Share this post


Link to post
Share on other sites
The only private member is ''head'' and that''s only accessed from the LinkedList class and that''s all ok. And the node''s members are declared public and access to them from LinkedList is therefore without problems.

Am I wrong or am I wrong?

Share this post


Link to post
Share on other sites
Seeing as there are no other options... you''re wrong.
(Even if I can''t back that up with actual proof)

This isn''''t life in the fast lane, it''''s life in the oncoming traffic.
-- Terry Pratchett

Share this post


Link to post
Share on other sites