Jump to content

  • Log In with Google      Sign In   
  • Create Account


java...pointers?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 nitishk   Members   -  Reputation: 161

Like
0Likes
Like

Posted 08 November 2012 - 11:06 PM

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, 08 November 2012 - 11:07 PM.


Sponsor:

#2 Bacterius   Crossbones+   -  Reputation: 8002

Like
2Likes
Like

Posted 08 November 2012 - 11:27 PM

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, 08 November 2012 - 11:29 PM.

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#3 frob   Moderators   -  Reputation: 18419

Like
2Likes
Like

Posted 08 November 2012 - 11:31 PM

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.
Check out my personal indie blog at bryanwagstaff.com.

#4 jbadams   Senior Staff   -  Reputation: 17031

Like
0Likes
Like

Posted 08 November 2012 - 11:34 PM

Provide mutator ("setter") methods? Make the variables public?

What are you actually trying to do?

#5 Lauris Kaplinski   Members   -  Reputation: 841

Like
0Likes
Like

Posted 09 November 2012 - 02:28 AM

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.
Lauris Kaplinski

First technology demo of my game Shinya is out: http://lauris.kaplinski.com/shinya
Khayyam 3D - a freeware poser and scene builder application: http://khayyam.kaplinski.com/

#6 nitishk   Members   -  Reputation: 161

Like
0Likes
Like

Posted 09 November 2012 - 11:20 AM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS