| | |
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:
Solved Threads: 501
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)
// network.java package AdHcTree2; import java.util.Vector; public class network { node* rootNode; Vector <node> netNodes; Vector <path> netPaths; public static void main (String args[]) { network theNetwork = new network (); theNetwork.CreatePaths(); theNetwork.DisplayParents (); } public network () { netNodes = new Vector (); netPaths = new Vector (); } node GetNode (int nodenum) { } void CreatePaths () { int numPaths = 17; int theArray[][] = { {3, 1}, {9, 7, 2, 1}, // 1 is root, 2 is child of 1, // 7 is child of 2, 9 is child of 9 {10, 8, 2, 1}, {11, 8, 2, 1}, {13, 12, 8, 2, 1}, {14, 12, 8, 2, 1}, {15, 14, 12, 8, 2, 1}, {16, 14, 12, 8, 2, 1}, {17, 14, 12, 8, 2, 1}, {18, 6, 4, 1}, {19, 6, 4, 1}, {20, 5, 4, 1}, {21, 5, 4, 1}, {22, 21, 5, 4, 1}, {23, 22, 21, 5, 4, 1}, {24, 23, 22, 21, 5, 4, 1}, {25, 23, 22, 21, 5, 4, 1} }; int arraySize[] = {2, 4, 4, 4, 5, 5, 6, 6, 6, 4, 4, 4, 4, 5, 6, 7, 7}; for (int i = 0; i < numPaths; i++) { path aPath = new path(this, arraySize[i], theArray[i]); netPaths.add(aPath); } } public void AddNodeToNetwork (node aNode) { } public void DisplayParents () { } } // node.java package AdHcTree2; import java.util.Vector; public class node { network* theNetwork; int nodeNumber; node* parent; Vector <node*> children; int depth; int column; public node () { nodeNumber = -1; } public node (node aNode) { } public node (int nodenumber, network* thenetwork) { } public node (int nodenumber, network* thenetwork, node* theParent, int theDepth) { } public void AddChild (node* child) { } } // path.java package AdHcTree2; import java.util.Vector; public class path { public Vector <node*> pathNodes; public path (network aNetwork, int numnodesinpath, int nodenum[]) { pathNode = new Vector (); // new nodes are created here with calls to node constructors } }
•
•
Join Date: Nov 2006
Posts: 224
Reputation:
Solved Threads: 31
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.
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.
•
•
Join Date: Jan 2008
Posts: 3,829
Reputation:
Solved Threads: 501
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.
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++?
In C++, object1 and object2 would both point to the same class1 object.
JAVA Syntax (Toggle Plain Text)
package aPackage; public class MyClass { public class1 object1; public class1 object2; public static void main(String[] args) { MyClass myc = new MyClass (); myc.foo (); } public void foo () { object1 = new class1 (1, 2); object2 = new class1 (3, 4); object1 = object2; System.out.println(object1.int1); System.out.println(object1.int2); System.out.println(object2.int1); System.out.println(object2.int2); } public MyClass () { } } class class1 { public int int1; public int int2; public class1 (int newint1, int newint2) { int1 = newint1; int2 = newint2; } }
Java Syntax (Toggle Plain Text)
class1* object1; class2* object2; object1 = new class1 (1, 2); object2 = new class1 (3, 4); object1 = object2;
•
•
Join Date: Jan 2008
Posts: 3,829
Reputation:
Solved Threads: 501
•
•
•
•
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.
•
•
•
•
yes. (except that in Java the first object is garbage-collected and in C++ it isn't)
Thank you both for replying. I will experiment around with this.
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.
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
If you prefer an offline copy, you can download it as a PDF here: http://mindview.net/Books/TIJ/DownloadSites
•
•
Join Date: Jan 2008
Posts: 3,829
Reputation:
Solved Threads: 501
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.
![]() |
Other Threads in the Java Forum
- Previous Thread: Java and Touchscreens
- Next Thread: Applet, thread
| Thread Tools | Search this Thread |
actuate android api applet application applications array arrays automation balls bank binary bluetooth business chat class classes clear client code codesnippet collections component database db defaultmethod development dice dragging draw ebook eclipse error event exception formatingtextintooltipjava fractal game givemetehcodez graphics gui hql html ide image infinite input integer invokingapacheantprogrammatically j2me java javaprojects jni jpanel julia linux list loop looping map method methods mobile mysql netbeans newbie openjavafx oracle parameter php print problem program programming project recursion repositories scanner screen scrollbar server set size sms sort sorting sql sqlserver state storm string sun superclass swing swt text-file threads time tree windows






