passing the address of a variable

Hi,How do you pass the address of a variable to a function ?
[74 byte] By [wmestdagha] at [2007-10-2 21:46:54]
# 1
Do you mean in a C++ pointer sort of way? Or are you just trying to pass a variable "by reference"?
Michael_Ebberta at 2007-7-14 1:02:32 > top of Java-index,Java Essentials,New To Java...
# 2

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 ?

wmestdagha at 2007-7-14 1:02:32 > top of Java-index,Java Essentials,New To Java...
# 3

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

warnerjaa at 2007-7-14 1:02:32 > top of Java-index,Java Essentials,New To Java...
# 4
> Hi,> > How do you pass the address of a variable to a> function ?You can't in Java.
jverda at 2007-7-14 1:02:32 > top of Java-index,Java Essentials,New To Java...
# 5

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

Michael_Ebberta at 2007-7-14 1:02:32 > top of Java-index,Java Essentials,New To Java...
# 6
> > 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"
georgemca at 2007-7-14 1:02:32 > top of Java-index,Java Essentials,New To Java...
# 7

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

jverda at 2007-7-14 1:02:32 > top of Java-index,Java Essentials,New To Java...
# 8

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

DrClapa at 2007-7-14 1:02:32 > top of Java-index,Java Essentials,New To Java...
# 9
I'm an old Assembler programmer. I don't miss pointers of any kind at all.
mfkean@aol.coma at 2007-7-14 1:02:32 > top of Java-index,Java Essentials,New To Java...
# 10
> I'm an old Assembler programmer. I don't miss> pointers of any kind at all.presumably you think pointers are for girls :-)
georgemca at 2007-7-14 1:02:32 > top of Java-index,Java Essentials,New To Java...
# 11
hi In java we can not pass a reference of variable. Because java does not provide call by referance like that C/C++ provide.Java does not have fully OOPS concept.
delhia at 2007-7-14 1:02:32 > top of Java-index,Java Essentials,New To Java...
# 12
Pass by reference has nothing to do with OO.
jverda at 2007-7-14 1:02:32 > top of Java-index,Java Essentials,New To Java...