pass by reference in java?

A

Angus

Hello

I am coming to Java from C++ . In C++, if I want to pass a variable into
another function then by default a copy of variable is passed. Is this the
same in Java?

I want to pass a reference to my variable to a java function. How do I do
that.

Reason is, in my function I have setup networking variables - but they are
not members of class. So I want to pass the networking variables, eg
socket, etc by reference to a connect function. How do I do that?

Angus
 
B

Bill Medland

Tor said:
No, the value (either pointer or primitive) is passed.


That's automatic - unless you're talking about a primitive. Java does
not have pointers to primitives, only to objects.


Just pass them as arguments.
But watch out for whether the object passed is mutable or not.

For example if you pass a string into a function and then change the string
in the function you won't see the change outside the function (because you
didn't actually change the string; you made the reference point at a
different string).
 
W

wesley.hall

For example if you pass a string into a function and then change the string
in the function you won't see the change outside the function (because you
didn't actually change the string; you made the reference point at a
different string).

We need to be very careful about wording here Bill....

If you pass a string reference into a method and then *point that
reference to a new string* the original reference will still point at
the first string. You cannot 'change' the string because strings are
immutable... but I think it was reassiging references that you meant.

To the OP... I find the easiest way to think about it is like this...

In java every primitive type is passed by value, an object references
can be considered as an additional primitive. Passing a reference into
a method will create a new reference that points the same instance of
the object. Reassigning the reference inside the method will be private
to the method but changing the state of the object will not.

As long as you think of references as another type of primitive, the
behaviour is pretty consistent.
 
T

Tor Iver Wilhelmsen

Angus said:
I am coming to Java from C++ . In C++, if I want to pass a variable into
another function then by default a copy of variable is passed. Is this the
same in Java?

No, the value (either pointer or primitive) is passed.
I want to pass a reference to my variable to a java function. How do I do
that.

That's automatic - unless you're talking about a primitive. Java does
not have pointers to primitives, only to objects.
Reason is, in my function I have setup networking variables - but they are
not members of class. So I want to pass the networking variables, eg
socket, etc by reference to a connect function. How do I do that?

Just pass them as arguments.
 
D

Dale King

Angus said:
It is a shame because in C++ you get used to using it.

But passing by reference is a very bad practice. In C++ you can get used
to using global variables, but it doesn't make them a good idea.
 
L

Lasse Reichstein Nielsen

Angus said:
I am coming to Java from C++ . In C++, if I want to pass a variable into
another function then by default a copy of variable is passed. Is this the
same in Java?

Technically, it's not the default way in C++. The default parameter
passing method passes a *value* to the function, not a *variable*. If
you use a variable as the function argument expression, the value of
that expression becomes the parameter.

However, in C++ you can pass a reference to a variable. In Java, you
can't.
I want to pass a reference to my variable to a java function. How do I do
that.

You can't. All parameter passing in Java is by value.

However, where C++ has objects as values that can be stored in
variables, Java only stores references to objects. These are the
values passed when you pass an object type to a method, not the
objects themselves.
Reason is, in my function I have setup networking variables - but they are
not members of class. So I want to pass the networking variables, eg
socket, etc by reference to a connect function. How do I do that?

Redesign. E.g., make a holder class for the variables and pass an instance
of that to your connect method. Or have the method return such a holder
object.

/L
 
A

Adam Maass

Angus said:
Hello

I am coming to Java from C++ . In C++, if I want to pass a variable into
another function then by default a copy of variable is passed. Is this
the
same in Java?

I want to pass a reference to my variable to a java function. How do I do
that.

Reason is, in my function I have setup networking variables - but they are
not members of class. So I want to pass the networking variables, eg
socket, etc by reference to a connect function. How do I do that?

This comes up so frequently.

Java is all pass-by-value.

However, all non-primitive values are, in fact, /references/ (which C++
people should read as pointers).


Unlike in C++, you cannot have a non-primitive variable that is not a
pointer.

I hope that helps.


-- Adam Maass
 

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,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top