Is there any analogue of C++'s &variable in Java ?

D

dkultasev

Hello,
in C++ I can do something like that

int a;

void test(int &a)
{
a = 1;
}

cout >> a;
//-------------------
a = 1

Is there any analogue in Java?

Sincerely,
Dmitrij
 
T

Thomas

Yes you can. You must use any referancable type such as
Integer, Character and so on. Then any method will be able to change such
variable if obtained. Read about refrences and java class types.

Integer a;

somwhere in class:

void test(Integer x){
x = 1;
}

System.out.println(x);

Hello,
 
I

Ingo R. Homann

Hi,
Yes you can. You must use any referancable type such as
Integer, Character and so on. Then any method will be able to change such
variable if obtained. Read about refrences and java class types.

Perhaps you should read about references as well! ;-)

Your example does not work of course:
Integer a;

somwhere in class:

void test(Integer x){
x = 1;
}

This will *not* change the value of the parameter of the caller. e.g.:

Integer x=2;
test(x);
System.out.println(x);

This will print "2" of course, and not "1".

Your example would work, if you had declared your method like this:

void test(MyInteger x) {
x.setValue(1);
}

Unfortunately, java.lang.Integer does not have a set-method (that's why
I called the parameter-type "MyInteger").

Of course, it is not difficult to implement such a class.

Ciao,
Ingo
 
D

dkultasev

....
int x = 0;
auto.test(x);
System.out.println(x);
....
public void test (int x)
{
x = 1;
}


Output: 0
 
C

Chris Dollin

Oy! You left their message languishing at the bottom of yours
where we can't see it to set the context!

(fx:relocation)
Yes you can. You must use any referancable type such as
Integer, Character and so on. Then any method will be able to change such
variable if obtained.

Integer and Character etc are immutable. You can't change them once
they're created.
Read about refrences and java class types.

Integer a;

somwhere in class:

void test(Integer x){
x = 1;
}

All /that/ will do is make the local variable `x` refer to an Integer
with value `1`. It won't change any argument passed to `test`.

You have to do it by hand -- create a mutable class that can hold
integer values and use instances of /that/.

final class AnInteger { public int value = 0; }

void test( AnInteger x ) { x.value = 17; }

... AnInteger y = new AnInteger();
test( y );
System.err.println( ">> " + y.value );
 
P

Patricia Shanahan

Thomas said:
Uzytkownik <[email protected]> napisal w wiadomosci
Yes you can. You must use any referancable type such as
Integer, Character and so on. Then any method will be able to change such
variable if obtained. Read about refrences and java class types.

Integer a;

somwhere in class:

void test(Integer x){
x = 1;
}

This is equivalent to:

void test(Integer x){
x = new Integer(1);
}

It changes which Integer object is referenced by the formal parameter x.
It has no effect on which Integer is referenced by a, or on a.intValue().

Instead, a needs to reference something that offers value-changing
syntax or methods. For example, you can assign to a specified element of
any int[] array:

void setOne(int[] data, int index){
data[index] = 1;
}

Similarly, if a references an object that has value-changing methods,
such as a StringBuilder:

void setOne(StringBuilder sb){
sb.setLength(0);
sb.append("ONE");
}

You can, of course, create a class of your own that has exactly the
value-changing methods you want.

Patricia
 
S

Stefan Ram

void test(int &a)

For reference parameters (»objects«), this already is the
default behavior in Java.

For primitive variables, one needs to emulate it by
wrapping the primitive variable into an object.

But sometimes it might be better to question the need
for this and find a solution where it is not needed.

(The subject line is misleading: In C++, »&identifier«
as an expression is the /address of/ an object. The
parameter specification »int &a« however, denotes a
reference parameter.)
 
C

Chris Dollin

Stefan said:
For reference parameters (»objects«), this already is the
default behavior in Java.

Not /quite/.

You can update the referred-to object, but you can't update the
/argument variable/.
 
S

Stefan Ram

Chris Dollin said:
Not /quite/.
You can update the referred-to object, but you can't update the
/argument variable/.

I agree. - I was thinking along the following lines:

In C++, when you have a parameter of object type, the whole
object will actually be copied:

example( ::std::string const text )

To avoid this, a reference parameter is used:

example( ::std::string const & text )

In Java, the passing of the reference instead of a copy of
the object already is the default behavior.
 
M

Mike Schilling

Hello,
in C++ I can do something like that

int a;

void test(int &a)
{
a = 1;
}

cout >> a;
//-------------------
a = 1

Is there any analogue in Java?

No. All arguments in Java are passed by value; there's no mechanism like

void test(int &a)

to cause an argument to be passed by reference. What can be a bit confusing
for a C++ programmer coming to Java is that a reference to an object is , in
C++ terms, a pointer. Consider Java syntax like

Class1 a = new Class1();
method(a);

The closest C++ to this would be

Class1 *a = new Class1();
method(a);

method() can of course make changes to the objects that a points to, but on
its return the value of the variable 'a" will be unchanged.
 
R

Roedy Green

int a;

void test(int &a)
{
a = 1;
}

cout >> a;
//-------------------
a = 1

Is there any analogue in Java?

the closest I can think of is this:

void test (Point p )
{
p.x = 10;
}
 
O

Owen Jacobson

the closest I can think of is this:

void test (Point p )
{
p.x = 10;

}

There's always the array hack (which I've used to simulate full-grown
closures, too). It's kind of abusive to the language, and you gain a
few more things that can go wrong, but:

void test (Point[] p) {
assert p.length == 1;
p[0] = new Point (10, p.y);
}

void test (int[] i) {
assert i.length == 1;
i[0] = 10;
}

-o
 

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
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top