Jump to content
  • Advertisement

Archived

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

Julio

[java] pointers in java

This topic is 5616 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
Advertisement
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
so uhh, is my linked list code i posted above correct?

[edited by - IllMind on March 3, 2003 12:11:18 AM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!