Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
dkultasev@gmail.com - 16 Nov 2007 13:00 GMT
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
Thomas - 16 Nov 2007 13:38 GMT
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,
> in C++ I can do something like that
>
[quoted text clipped - 13 lines]
> Sincerely,
> Dmitrij
Ingo R. Homann - 16 Nov 2007 14:05 GMT
Hi,

Thomas schrieb:
> 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;
>
[quoted text clipped - 3 lines]
> 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
dkultasev@gmail.com - 16 Nov 2007 14:07 GMT
...
int x = 0;
auto.test(x);
System.out.println(x);
...
public void test (int x)
{
 x = 1;
}

Output: 0
Chris Dollin - 16 Nov 2007 14:25 GMT
> ...
> int x = 0;
[quoted text clipped - 7 lines]
>
> Output: 0

And?

Signature

Chris "bated breath" Dollin

Hewlett-Packard Limited     Cain Road, Bracknell,                registered no:
registered office:          Berks RG12 1HN                       690597 England

Chris Dollin - 16 Nov 2007 14:18 GMT
Oy! You left their message languishing at the bottom of yours
where we can't see it to set the context!

(fx:relocation)

>> Hello,
>> in C++ I can do something like that
[quoted text clipped - 11 lines]
>>
>> Is there any analogue in Java?

> 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.
>
[quoted text clipped - 5 lines]
> 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 );

Signature

Hewlett-Packard Limited                                          registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN          690597 England

Patricia Shanahan - 16 Nov 2007 14:24 GMT
> 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
[quoted text clipped - 7 lines]
> 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

> System.out.println(x);
>
[quoted text clipped - 16 lines]
>> Sincerely,
>> Dmitrij
Stefan Ram - 16 Nov 2007 15:54 GMT
>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.)
Chris Dollin - 16 Nov 2007 15:57 GMT
>>void test(int &a)
>
>   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/.

Signature

Chris "archaic" Dollin

Hewlett-Packard Limited     Cain Road, Bracknell,                registered no:
registered office:          Berks RG12 1HN                       690597 England

Stefan Ram - 16 Nov 2007 16:16 GMT
>>>void test(int &a)
>>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/.

 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.
Mike Schilling - 16 Nov 2007 22:11 GMT
> Hello,
> in C++ I can do something like that
[quoted text clipped - 11 lines]
>
> 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.
Roedy Green - 17 Nov 2007 07:37 GMT
>int a;
>
[quoted text clipped - 8 lines]
>
>Is there any analogue in Java?

the closest I can think of is  this:

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

Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Owen Jacobson - 18 Nov 2007 01:06 GMT
On Nov 16, 11:37 pm, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:
> On Fri, 16 Nov 2007 05:00:27 -0800 (PST), dkulta...@gmail.com wrote,
> quoted or indirectly quoted someone who said :
[quoted text clipped - 19 lines]
>
> }

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
Andrey Ryabov - 18 Nov 2007 08:50 GMT
May be AtomicInt AtomicLong AtomicWhatsoever would halped you?


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.