• Advertisement
Sign in to follow this  

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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


Starting with line 5, you create a new object. This allocates the object, setting the myFoo pointer. Let's say it creates an object at memory address 32. Line 6 sets the name string of the object at memory address 32. Line 7 calls the function; the pointer is passed by value --- that is, you can modify the thing being pointed to (the object at address 32), but you do not modify the pointer, it will still point to memory address 32. Line 1 makes a new pointer, called bar, that is initialized to point to address 32, which is the pass-by-value. Line 2 modifies the name of the object stored at memory address 32. Then line 3 creates a new object, say at memory address 70, and points the new bar pointer to the new memory address. Line 4 modifies the name of the object stored at memory address 70. When we return to line 8, since myFoo was passed by value, it still points to memory address 32, so the object was modified within the function, but it was not replaced by the function.

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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement