newbie question on refrences.

J

JustSomeGuy

I'm a C++ programmer moving towards writing some java applications.
I'm quite familiar with Java, but I'm a little muddled over passing by
value,
vs passing by refrence.

In C++:

int & function(int x, int &y)

the function may modify x but the changes will not effect the callers value
of x.
the function may modify y and the changes will effect the callers value of
y.

the return is a refrence to an integer within the function and a copy of it
is
made in the callers scope.

As I understand it everything in Java is a reffrence. So how does this work?
If everything is a refrence then what is the point of having parameters, why
not
just use global variables?
 
A

A. Bolmarcich

I'm a C++ programmer moving towards writing some java applications.
I'm quite familiar with Java, but I'm a little muddled over passing by
value,
vs passing by refrence.
[snip]

As I understand it everything in Java is a reffrence. So how does this work?
If everything is a refrence then what is the point of having parameters, why
not
just use global variables?

Not everything in Java is a reference. Java has primitive data types,
such as int and double, and references to Objects, such as instances of
classes java.lang.Integer and java.lang.Double. All arguments are
passed by value. Because Java, unlike C++, does not have Object
variables or parameters, Objects are never passed as arguments,
Object references are.
 
X

xarax

JustSomeGuy said:
I'm a C++ programmer moving towards writing some java applications.
I'm quite familiar with Java, but I'm a little muddled over passing by
value,
vs passing by refrence.

In C++:

int & function(int x, int &y)

the function may modify x but the changes will not effect the callers value
of x.
the function may modify y and the changes will effect the callers value of
y.

the return is a refrence to an integer within the function and a copy of it
is
made in the callers scope.

As I understand it everything in Java is a reffrence. So how does this work?
If everything is a refrence then what is the point of having parameters, why
not
just use global variables?

In C++, a "reference" is syntactical sugar for a pointer. NOTHING MORE.

Java does not have pointers, therefore Java does not have C++ references.

In Java, a "reference" means an object instance. Sometimes called a pointer
or a handle, it is an indirect path to the object allocated on the heap. Java
does not have C pointers (no pointer arithmetic).

Java, C, and C++ DO NOT HAVE pass by reference. They all have pass
by value. The C++ "reference" is handled as a pointer, and the value
of that pointer is passed by value to a called method.

If you want the same effect of multiple "out" parameters, then use
a Java container class. Create an object of the container class, and
pass it to the method. The method fills in the container object with
whatever and returns to the caller. The caller extracts the data from
the container object.
 
G

Gary Labowitz

xarax said:
"JustSomeGuy" <[email protected]> wrote in message

In C++, a "reference" is syntactical sugar for a pointer. NOTHING MORE.

I believe that statement to be false. There is no requirement for a
reference to be implemented as a pointer. But this would be more properly
discussed in a C++ group, where more people know what they are talking about
regarding C++ references.
 
J

Joona I Palaste

I believe that statement to be false. There is no requirement for a
reference to be implemented as a pointer. But this would be more properly
discussed in a C++ group, where more people know what they are talking about
regarding C++ references.

The statement is indeed false. In C++, pointers and references are
different things. A C++ pointer is exactly like a Java reference. (Well,
apart from pointer arithmetic, which Java does not implement.) A C++
reference, OTOH, must be bound to a variable at defination, and stays
bound to it for its entire lifetime. Any changes to the reference are
reflected in the actual variable, even if they are in different scopes.

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"A bee could, in effect, gather its junk. Llamas (no poor quadripeds) tune
and vow excitedly zooming."
- JIPsoft
 
C

Chad

Alright when you make a reference to something it will follow this format.
Object x;
This is making a
But then you have to instantiate it to make it worth anything, ex:
x = new Object();
The new command is returning a memory location to x;
These two actions may be combined into one step:
You can combine the 2 steps like this:
Object x =new Object();
So now you have x --->[object @ memory location]

Now the objious thing about references is that when you say y=x it is just
making y a reference to that same object. It's easy to remember because x is
just a memory location. Nothing more.
So it's like:
x(memory location, say 27 for the hell of it)------>[object @ memory
location]
Y(27)------------------------------------------------------^
so now say you change whatever value of whatever x is to something like
boob, then the value of y is now boob and vice versa.

primitives and references to objects are passed (to
methods) by value. Pass by value is when a temporary copy of the
value is made to send to the called method. This copy is destroyed
when the called method returns. Any changes to the parameter itself
do not affect its value in the calling method because it is a temporary
copy. When a reference is passed in to a method, the object to
which it is referring is not copied thus changes made to the object WILL
cause permanent or persistent changes.


A full explanation of this should be found in some hardcore book or
something. It'd probably be a better explanation as well. Oh well, I hope
this might help a little.
 

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,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top