Can I use pointers to non-primitive types in JAVA?

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jan 2008
Posts: 3,829
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Can I use pointers to non-primitive types in JAVA?

 
0
  #1
Mar 14th, 2008
I'm pretty sure the answer is a definite no if it is a pointer to a primitive type, but I have created classes called "network", "path", and "node" and I want them all to be able to refer to each other. These are not primitive types, so I hope I can point to them somehow. I don't know the syntax to do that since it appears to be much different from C++ syntax. I am starting with 17 arrays of integers representing paths from leaf nodes to the root node in an ad hoc wireless system. My goal is to build a tree and represent it graphically. I have 25 nodes total, so I'd like to only create 25 node objects, but still have the network and path classes be able to refer to these nodes when building the tree. There are other classes too, but I hope this is enough to demonstrate the problem. I have set up a C++-style vector of pointers to objects, which Java is rejecting. Since these are not primitive types, I can use pointers in some way, correct? Thanks.

  1. // network.java
  2. package AdHcTree2;
  3.  
  4. import java.util.Vector;
  5.  
  6. public class network
  7. {
  8. node* rootNode;
  9. Vector <node> netNodes;
  10. Vector <path> netPaths;
  11.  
  12.  
  13. public static void main (String args[])
  14. {
  15. network theNetwork = new network ();
  16. theNetwork.CreatePaths();
  17. theNetwork.DisplayParents ();
  18. }
  19.  
  20.  
  21. public network ()
  22. {
  23. netNodes = new Vector ();
  24. netPaths = new Vector ();
  25. }
  26.  
  27.  
  28. node GetNode (int nodenum)
  29. {
  30. }
  31.  
  32.  
  33. void CreatePaths ()
  34. {
  35. int numPaths = 17;
  36. int theArray[][] =
  37. {
  38. {3, 1},
  39. {9, 7, 2, 1}, // 1 is root, 2 is child of 1,
  40. // 7 is child of 2, 9 is child of 9
  41. {10, 8, 2, 1},
  42. {11, 8, 2, 1},
  43. {13, 12, 8, 2, 1},
  44. {14, 12, 8, 2, 1},
  45. {15, 14, 12, 8, 2, 1},
  46. {16, 14, 12, 8, 2, 1},
  47. {17, 14, 12, 8, 2, 1},
  48. {18, 6, 4, 1},
  49. {19, 6, 4, 1},
  50. {20, 5, 4, 1},
  51. {21, 5, 4, 1},
  52. {22, 21, 5, 4, 1},
  53. {23, 22, 21, 5, 4, 1},
  54. {24, 23, 22, 21, 5, 4, 1},
  55. {25, 23, 22, 21, 5, 4, 1}
  56. };
  57.  
  58. int arraySize[] = {2, 4, 4, 4, 5, 5, 6, 6, 6, 4, 4, 4, 4, 5, 6, 7, 7};
  59. for (int i = 0; i < numPaths; i++)
  60. {
  61. path aPath = new path(this, arraySize[i], theArray[i]);
  62. netPaths.add(aPath);
  63. }
  64. }
  65.  
  66.  
  67. public void AddNodeToNetwork (node aNode)
  68. {
  69. }
  70.  
  71.  
  72. public void DisplayParents ()
  73. {
  74. }
  75. }
  76.  
  77.  
  78. // node.java
  79. package AdHcTree2;
  80. import java.util.Vector;
  81.  
  82. public class node
  83. {
  84. network* theNetwork;
  85. int nodeNumber;
  86. node* parent;
  87. Vector <node*> children;
  88. int depth;
  89. int column;
  90.  
  91. public node ()
  92. {
  93. nodeNumber = -1;
  94. }
  95.  
  96.  
  97. public node (node aNode)
  98. {
  99. }
  100.  
  101.  
  102. public node (int nodenumber, network* thenetwork)
  103. {
  104. }
  105.  
  106.  
  107. public node (int nodenumber, network* thenetwork, node* theParent, int theDepth)
  108. {
  109. }
  110.  
  111.  
  112. public void AddChild (node* child)
  113. {
  114. }
  115. }
  116.  
  117.  
  118. // path.java
  119. package AdHcTree2;
  120. import java.util.Vector;
  121.  
  122. public class path
  123. {
  124. public Vector <node*> pathNodes;
  125.  
  126. public path (network aNetwork, int numnodesinpath, int nodenum[])
  127. {
  128. pathNode = new Vector ();
  129. // new nodes are created here with calls to node constructors
  130. }
  131. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 224
Reputation: bugmenot is an unknown quantity at this point 
Solved Threads: 31
bugmenot bugmenot is offline Offline
Posting Whiz in Training

Re: Can I use pointers to non-primitive types in JAVA?

 
0
  #2
Mar 14th, 2008
you should read a basic guide to Java

Java consists only of primitive types and reference types (which are kind of like pointers in C). Reference types point to objects, and are named after the class of objects they point to. Objects are not values in Java; and are always manipulated via references.
Last edited by bugmenot; Mar 14th, 2008 at 11:04 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,829
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Can I use pointers to non-primitive types in JAVA?

 
0
  #3
Mar 15th, 2008
I realized that my example with the '*' syntax wouldn't work. My intent was to show the C-Style code and find out if there is a way to do something similar in Java. I'm wondering if the following program is the Java equivalent of C++-style pointer assignment.
  1. package aPackage;
  2.  
  3. public class MyClass
  4. {
  5. public class1 object1;
  6. public class1 object2;
  7.  
  8. public static void main(String[] args)
  9. {
  10. MyClass myc = new MyClass ();
  11. myc.foo ();
  12. }
  13.  
  14.  
  15. public void foo ()
  16. {
  17. object1 = new class1 (1, 2);
  18. object2 = new class1 (3, 4);
  19. object1 = object2;
  20.  
  21. System.out.println(object1.int1);
  22. System.out.println(object1.int2);
  23. System.out.println(object2.int1);
  24. System.out.println(object2.int2);
  25. }
  26.  
  27.  
  28. public MyClass ()
  29. {
  30. }
  31. }
  32.  
  33.  
  34. class class1
  35. {
  36. public int int1;
  37. public int int2;
  38.  
  39.  
  40. public class1 (int newint1, int newint2)
  41. {
  42. int1 = newint1;
  43. int2 = newint2;
  44. }
  45. }
I got the desired output (3, 4, 3, 4). Stepping through the debugger, before line 19 above was executed, NetBeans assigned a "value" of #44 to object1 and #47 to object2. I am guessing that those are the "references" you are referring to. Can I assume that #44 and #47 correspond to addresses in memory (I'd like to say they "point to addresses" but I'm not sure if that's the correct interpretation)? After line 19 is executed, both objects had "values" of #47. #44 no longer appeared in the debugger anywhere. object1 and object2 had the same values for int1 and int2 (3 and 4 respectively). So is this the equivalent of the following in C++?
  1. class1* object1;
  2. class2* object2;
  3. object1 = new class1 (1, 2);
  4. object2 = new class1 (3, 4);
  5. object1 = object2;
In C++, object1 and object2 would both point to the same class1 object.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Can I use pointers to non-primitive types in JAVA?

 
0
  #4
Mar 15th, 2008
and references are always passed by value (unlike pointers in C which can be passed by reference) and cannot be manipulated by something like pointer arithmetic.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 224
Reputation: bugmenot is an unknown quantity at this point 
Solved Threads: 31
bugmenot bugmenot is offline Offline
Posting Whiz in Training

Re: Can I use pointers to non-primitive types in JAVA?

 
1
  #5
Mar 15th, 2008
Originally Posted by VernonDozier View Post
So is this the equivalent of the following in C++?
yes. (except that in Java the first object is garbage-collected and in C++ it isn't)
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,829
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Can I use pointers to non-primitive types in JAVA?

 
0
  #6
Mar 15th, 2008
Originally Posted by jwenting View Post
and references are always passed by value (unlike pointers in C which can be passed by reference) and cannot be manipulated by something like pointer arithmetic.
I think I'm okay on not being allowed to use pointer arithmetic because I don't think it's going to come up in this program. I think that the "pass by value" restriction won't be a problem either, but I haven't gotten there yet.
Originally Posted by bugmenot View Post
yes. (except that in Java the first object is garbage-collected and in C++ it isn't)
That is what I suspected, but thank you for confirming. That is good news. If need be I can create extra nodes temporarily and change references later and Java figures out what is no longer needed and throws it away, so I don' t have a lot of extra nodes to keep track of. I was keeping all of my nodes with redundant information earlier and doing a lot of deep copies, which seemed wasteful.


Thank you both for replying. I will experiment around with this.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Can I use pointers to non-primitive types in JAVA?

 
1
  #7
Mar 15th, 2008
yes, anything to which there are no more references will be deleted by the garbage collector at some point. No need to keep track of that yourself and do things like malloc, calloc, and realloc.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,485
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 517
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Can I use pointers to non-primitive types in JAVA?

 
1
  #8
Mar 15th, 2008
You might want to take a look at the free online book Thinking In Java, 3rd Edition. It is a bit dated since it doesn't cover any of the Java 1.5 or 1.6 changes, but the language basics are still solid and you'd probably find it a good reference for questions like this.
If you prefer an offline copy, you can download it as a PDF here: http://mindview.net/Books/TIJ/DownloadSites
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,829
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Can I use pointers to non-primitive types in JAVA?

 
0
  #9
Mar 16th, 2008
I successfully completed my program. I was able to use pointer-type logic, though I realize that one should not refer to references as pointers. I'm going to read up a bit more on the technical differences between the two. I was able to do what I normally did in C++ in similar situations, just differently. Thank you all for the tips and I will check out that link you posted, Ezzaral.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC