Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualBacterius

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

#2Bacterius

Posted 08 November 2012 - 11:28 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.

#1Bacterius

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 (it's also a memory leak)
}

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.

PARTNERS