[java] pointers in java

Started by
15 comments, last by Julio 21 years, 1 month ago
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
My HomepageSome shoot to kill, others shoot to mame. I say clear the chamber and let the lord decide. - Reno 911
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
Over the centuries, mankind has tried many ways of combating the forces of evil...prayer,fasting, good works and so on. Up until Doom, no one seemed to have thought about thedouble-barrel shotgun. Eat leaden death, demon... -- Terry Pratchett
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]

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
cool, thanks guys.

My Homepage
My HomepageSome shoot to kill, others shoot to mame. I say clear the chamber and let the lord decide. - Reno 911
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
Over the centuries, mankind has tried many ways of combating the forces of evil...prayer,fasting, good works and so on. Up until Doom, no one seemed to have thought about thedouble-barrel shotgun. Eat leaden death, demon... -- Terry Pratchett
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
refrain_from_stupidity( &me );
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.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
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.
so uhh, is my linked list code i posted above correct?

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

This topic is closed to new replies.

Advertisement