java...pointers?

This topic is 1902 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

So i have started learning java at my school. For most people this is their first language but I already know C so my knowledge in that is helping but I think sometimes it is limiting as well. In C there is so many times where pointers are used but how would I do the same in java? I did some research but all I could find is that referencing is only used in passing objects between methods. I would like to change the value of 3 int variables initialized in method A but instead of making them global (which I've been told is bad programming) I would like to just pass their locations in memory so I can change the variables in method B. What would be the correct way of doing something like this in java? Edited by chosenkill6

Share on other sites
Sorry. No pointers in Java. But you can use references, like in C++. For instance, you can also put your three variables into a custom-made class, kind of like a struct in C, and pass that to the function. You might be tempted to use the Integer class, which is a wrapper around the primitive int, but you must be careful - this class is immutable which can lead to rather unintuitive behavior when passing by reference.

In Java, primitive types are passed by value, and classes are passed by reference (which means a reference of them, is passed by value).

Suppose you pass an instance your class LinkedNode (or whatever) to some function:
 void doStuff(LinkedNode node); ... void main() { LinkedNode myNode = new LinkedNode(blah); doStuff(myNode); // what does myNode look like now? } 

Then, you are passing the memory address of "node" (which is a reference) by value (with some syntactic sugar on top), which means that:

 void doStuff(LinkedNode node) { node.change(...); // this will reflect on "node" outside the function } 

But:
 void doStuff(LinkedNode node) { node = createNewNode(); // will NOT reflect outside the function } 

Because in the first case, you will have altered the reference, but in the second case, you will just have changed the value.

This is the same as in C. For C, you will have the equivalent (assuming LinkedNode is a struct):

 void doStuff(LinkedNode *node); ... void main() { LinkedNode *myNode = malloc(sizeof(LinkedNode)); // and initialize fields as needed doStuff(myNode); // what does myNode look like now? } 

And:

 void doStuff(LinkedNode *node) { node->someField = 3; // this will change someField of the node outside the function } 

But:

 void doStuff(LinkedNode *node) { // make a new node for some reason *node = malloc(sizeof(LinkedNode)); // will not change the node at all since you're just changing the pointer (it's also a memory leak, btw) } 

Now your problem is that int is a primitive type, so it is *always* passed by value. As I said above, you can use the wrapper class Integer, but unfortunately it is immutable, which means you cannot do anything useful with its reference. You can write your own mutable integer class, which will work, but is rather heavyweight. It is best to have a class that contains all your stuff (in your case, your three variables) so that you don't have to manage one class everytime you need to pass an integer around methods.

Another possibility to consider is that having a method modifying three integers may not be very Java-like, and you may be better served by splitting the work into multiple methods, each returning a single integer, so that you can combine them later on. But it depends on what you are trying to do. Edited by Bacterius

Share on other sites
In java all objects are basically pointers. You allocate the memory with new, and the memory is freed by the garbage collector.

Foo myFoo = new Foo();

myFoo is called an object, but is actually a pointer to an object.

Java passes objects as references passed by value.

You can modify the contents of the object and they will remain modified when you return. For example:

TestFunc ( Foo bar ) //1
{
bar.setName ("replaced name"); //2
bar = new Foo(); //3
bar.setName("new object"); //4
}

Foo myFoo = new Foo(); //5
myFoo.setName("original"); //6
TestFunc(myFoo); //7
myFoo.name.equals("replaced name") // 8

To modify your integers you cannot pass them by value and have those values change, but you can create an object that contains three integers and modify those members from within the function.

Share on other sites
Provide mutator ("setter") methods? Make the variables public?

What are you actually trying to do?

Share on other sites
Another possibility is making these 3 integers into 3-element integer array. Arrays are normal objects and are passed by reference (value of reference).

But it is hard to say, what is the "right" thing to do without having proper context.

Share on other sites
Thanks for the replies guys

What I am trying to do is get input and put that into the variables so I would like to have a separate method that gets input and then another that manipulates the variables.

I understand that I can put the integers into an object but it seems like overkill since I only need to pass the three variables to one function which will get input from the user and put the value into the variable. For now I have them as global so I'm not passing anything. Are global variables really that bad that I should go through all this trouble and change it?

I think I may just go with returning an array from the method but since the variables are so different in purpose within the program it will be hard to keep track of.