Hi,
Sorry your reply is not clear. I mean the address of the var, so 'by reference' is maybe the good english literral. I try to explain with a C example:
int test;
changeTest(&test);
Function changeTest is given in EAX register not the value of 'test', but the address of 'test'. Is this more clear ?
> Hi,
>
> Sorry your reply is not clear. I mean the address of
> the var, so 'by reference' is maybe the good english
> literral. I try to explain with a C example:
>
> int test;
> changeTest(&test);
>
>
> Function changeTest is given in EAX register not the
> value of 'test', but the address of 'test'. Is this
> more clear ?
There is no such thing as pass-by-reference in Java. There doesn't need to be such a thing either. You can design your code differently. For example, you could have a class which wraps that integer value, and pass an object of that class to the method (by value, as all parameters are passed). In the method, you can change the integer value that the object wraps.
Or, in this simple case, the method could return an integer value instead of trying to accept one by reference.
Here is a class that shows you aren't modifying the original primitive inside your function:
public class MethodCall {
public static void main(String[] args) {
int x=2;
int y = multiplyByFive(x);
System.out.println("2 times 5 is " + y); /* the result will be 10 */
System.out.println("x = " + x); /* x is still 2 */
}
private static int multiplyByFive(int p_incomingInt) {
return p_incomingInt * 5;
}
}
If you want to modify the original value, you can assign it to the return value of the method call, such as
x = mutliplyByFive(x);
changed a variable name
Michael_Ebbert
> > > Hi,
> > >
> > > How do you pass the address of a variable to a
> > > function ?
> >
> > You can't in Java.
>
> I'd say you don't need to. only once have I
> ever thought to myself "god, I miss pointers"
Good point.
My own experience is similar, maybe +/- one time.
> > I'd say you don't need to. only once have
> I
> > ever thought to myself "god, I miss pointers"
>
> Good point.
>
> My own experience is similar, maybe +/- one time.
I found myself wanting pass-by-reference once. But that was when I was converting a Visual Basic program to Java. The result was the ugliest Java program I ever wrote.