Java C++ reference equivalent

M

Martin Johansen

Hi grp

I am creating a function to add two numbers without using a return value.
But I cannot find a method to pass a reference to an integer. Thanks:

public static void iadd(int &a, int b, int c){
a = b + c;
}
 
B

Bjorn Abelli

...
I am creating a function to add two numbers without using a return value.
But I cannot find a method to pass a reference to an integer. Thanks:

You cannot pass a variable of a primitive type by reference, actually you
can't pass *anything* by reference in Java. Everything is passed by value,
but the value of a "reference type" is a reference to the object...
public static void iadd(int &a, int b, int c){
a = b + c;
}

Why you would want that behavior goes a bit over my head.

Usually when "converting" code from one language to another, one use to look
more to "what am I trying to accomplish?", "are there other and maybe better
ways to do this in the new language?", etc.

Depending on what that method *actually* needs to do in your program,
there's two solutions from the top of my head.

-------------------------------

The first and I guess most obvious, is to skip the requirement that the
method isn't allowed to give a return value.

public static int iadd(int b, int c) {
return b + c;
}

-------------------------------

The second approach is to encapsulate the "return value" in a wrapper of
some kind.

An example:

class IntHolder
{
public int value;
}

....

public static void iadd(IntHolder a, int b, int c) {
a.value = b + c;
}

....


// Bjorn A
 
S

shakah

Not sure if things have changed since 1.4, but I think you're stuck
wrapping the first argument in an object, e.g.

public class inttest {
public static void main(String [] asArgs) {
MyInt mi = new MyInt() ;
iadd(mi, 5, 7) ;
System.out.println("iadd(mi, 5, 7): " + mi) ;
}

public static class MyInt {
private int n_ ;

public MyInt() { }
public MyInt(int n) { n_ = n ; }
public int value() { return n_ ; }
public int value(int n) { return n_ = n ; }
public String toString() { return ""+n_ ; }
}

public static void iadd(MyInt mi, int a, int b) {
mi.value(a + b) ;
}
}
 
S

Seamus

Technically - it's not a function, but that's technical theoretical
nit-pick.

And now to answer your question :

No. Not as given.

What's wrong with :

int iadd(int b, int c){
return b+c;
}
 
P

Phil Staite

Martin said:
Hi grp

I am creating a function to add two numbers without using a return value.
But I cannot find a method to pass a reference to an integer. Thanks:

public static void iadd(int &a, int b, int c){
a = b + c;
}

Per the other suggestions, wrap the primitive in an object. But I
wouldn't bother writing my own wrapper, what's wrong with
java.lang.Integer ???
 
M

Malte

Phil said:
Per the other suggestions, wrap the primitive in an object. But I
wouldn't bother writing my own wrapper, what's wrong with
java.lang.Integer ???

What would you do with the Integer object? It cannot be used in the way
the op wishes.

You can pass it to a method, and then what? It has no modifier method.
 
B

Bjorn Abelli

Phil Staite wrote:

I think you refer to my "IntHolder" example?
What would you do with the Integer object? It cannot be
used in the way the op wishes.

You can pass it to a method, and then what?
It has no modifier method.

Exactly, that's why I suggested an "IntHolder", and if you don't want to
write your own, there actually is one in the library...

;-)

// Bjorn A
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top