943,634 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1254
  • Java RSS
Mar 14th, 2008
0

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

Expand Post »
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.

Java Syntax (Toggle Plain Text)
  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. }
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Mar 14th, 2008
0

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

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.
Reputation Points: 53
Solved Threads: 33
Posting Whiz in Training
bugmenot is offline Offline
224 posts
since Nov 2006
Mar 15th, 2008
0

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

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.
JAVA Syntax (Toggle Plain Text)
  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++?
Java Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Mar 15th, 2008
0

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

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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Mar 15th, 2008
1

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

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)
Reputation Points: 53
Solved Threads: 33
Posting Whiz in Training
bugmenot is offline Offline
224 posts
since Nov 2006
Mar 15th, 2008
0

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

Click to Expand / Collapse  Quote originally posted by jwenting ...
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.
Click to Expand / Collapse  Quote originally posted by bugmenot ...
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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Mar 15th, 2008
1

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

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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Mar 15th, 2008
1

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

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
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007
Mar 16th, 2008
0

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

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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Java and Touchscreens
Next Thread in Java Forum Timeline: Applet, thread





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC