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.

// 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
    }
}

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.

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.

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;
    }
}

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++?

class1* object1;
class2* object2;
object1 = new class1 (1, 2);
object2 = new class1 (3, 4);
object1 = object2;

In C++, object1 and object2 would both point to the same class1 object.

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.

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)

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.

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.

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.

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.