R
Roedy Green
get 47 from System.out.println(j.i);
what should I do to get 65?
see http://mindprod.com/jgloss/callbyvalue.html
get 47 from System.out.println(j.i);
what should I do to get 65?
1) in the real program I change 3 different objects of 3 different types in
my method and all I want to do is to return one of them and change in the
method the 2 others so that the changes are seen externally.
2) what is more, I want to learn how to do a thing like that in Java. In
C/C++ there's no problem with doing so,
Then you have a very bad design. You need to fix the design, not try to
figure out how to get Java to support your bad design.
What you need to do is learn to program, not hack. You seem to be
driven by ego, which is why you are getting responses about childhood
trauma. By the nature of your questions, your approaches to your
various problems are seriously flawed -- in any language.
true. That's why I said "In short"... I generally think of objects as
references anyways, so I get confused when trying to explain.
is asking how to translate "hot dog" into French, and insisting that
"chien chaud" is correct, even though that means literally in French
an overheated canine.
rowla said:1) in the real program I change 3 different objects of 3 different types in
my method and all I want to do is to return one of them and change in the
method the 2 others so that the changes are seen externally.
This is a recurring topic. The consensus it pays to be nit picky in
language. If you are very rigid, people suddenly get it. Anything
you do to simplify, just confuses them. So we rigidly say, Java
always passes by value -- references and primitives. You can't pass
an object, only a reference to it.
Tim said:rowla,
good question... the trick is to understand that in Java
private void f(Z j) {
does not equate to a C method of
private void f(Z *j) {
but rather more like another level of indirection
private void f(Z **j) {
j=&(new Z(65));
// or something like *(&j)=new Z(65); in the original.
In short, this does not work in Java as a C progammer would expect.
Daniel Sjöblom said:Your problem is not related to pointers.
void f(Z j) { j.i = 65; }
There are no value objects in java. Z is a reference passed by value.
References are similar to C pointers, except no pointer arithmetic (the
. operator dereferences, similar to -> in C). To simulate C style:
void f(struct Object **o) { *o = malloc(sizeof(struct Object)); }
you wrap your Object in a wrapper in java:
class Wrapper
{
Object o;
}
void f(Wrapper w) { w.o = new Object(); }
Java and C are both pass by value only. You can't change the value of a
passed pointer in a C function either (that is, it won't affect the
pointer from the view of the caller).
I have had a few people say this (C is only pass be value) because
the value you are passing is an adress and you can't change the
address. That argument doesn't make sense to me. Would you say
only that C++ has something other than pass by value?
I think most C++ implementations will still pass the value as an
adrress when passing a reference, so in reality you are still
passing by value.
(I don't see how there is really any difference, except the
language semantics).
Is that pass be reference then - so C does have pass by reference?
Why do people even argue about such a useless topic (pass by value or
pass by reference)? It shouldn't really matter.
Bryan said:I have had a few people say this (C is only pass be value) because the
value
you are passing is an adress and you can't change the address. That
argument
doesn't make sense to me. Would you say only that C++ has something
other than pass by value?
I think most C++ implementations will still
pass the value as an adrress when passing a reference, so in reality
you are still passing by value.
Ultimately you are probably just copying some 32bits into a register
and modifying that won't affect the caller. To affect the caller you
are modifying a value through another value, address, reference or
object (I don't see how there is really any difference, except the
language semantics).
In c I could create a macro to make it like im
passing by reference.
#include <stdio.h>
#define pass_by_ref(A) _call_it(&(A))
void _call_it(int * a) {
*a = 0;
}
int main (void) {
int value;
value = 10;
printf("value = %d\n", value);
pass_by_ref(value);
printf("value = %d\n", value);
return 0;
}
Is that pass be reference then - so C does have pass by reference?
Why do people even argue about such a useless topic (pass by value or
pass by reference)? It shouldn't really matter.
Lasse Reichstein Nielsen said:I have had a few people say this (C is only pass be value) because
the value you are passing is an adress and you can't change the
address. That argument doesn't make sense to me. Would you say
only that C++ has something other than pass by value?
Of C, C++ and Java, yes. There is a slight, semantic, difference
between passing a pointer to a value and passing a reference to a
variable. Sure you can implement the latter with the former, but
not the other way around. When passing a reference, you don't get
access to the pointer.
I think most C++ implementations will still pass the value as an
adrress when passing a reference, so in reality you are still
passing by value.
What the implementation does is not the point. It's probably machine
code anyway, where there are no pointers or references, just bit
patterns. The difference is what it looks like in the language. In C++
you can write
void foo(int &x) {
x++;
}
...
int y = 24;
foo(y);
Inside foo, x is just another variable. Outside foo, y is just another
variable. It is the way the paramter is passed that makes the difference.
Neither Java nor C have anything resembling this. C can pass pointers,
but then the pointer becomes the value. You need to explicitly
dereference it to affect the memory cell it is pointing to. Java
has nothing.
Notice that both C++ and Java have mutable structures that can be
passed as values and changed (C actually passes struct's as values).
That is not the same as a reference parameter.
(I don't see how there is really any difference, except the
language semantics).
That *is* the point. It makes a difference in what you can do, how
easy you can do it, and what the compiler can assume. You can do better
optimizations when you know that references to variables can't be
passed around. Java is designed to be safe, something you can never
be with first class pointers. Take this C program:
---
void foo() {
int i=42;
return &i;
}
void bar(int *p) {
int i;
printf("%d - %d\n",*p,i);
}
int main(int argc,char *argv[]) {
bar(foo());
}
---Is that pass be reference then - so C does have pass by reference?
No, that is an encoding that works like it. You can also encode objects
in C, but that doesn't make it object oriented.
Would you say only that C++ has something
other than pass by value?
Sure the language may not be object oriented, however I
can design applications using objects and still implement them in C.
Consider that Bjarne Stroustrup's first C++ compiler generated C code.
So obviously you can do OO in C, it is just a lot uglier.
Chris Smith said:Dale's right, but http://jinx.swiki.net/66 might at least point in the
right direction for finding a solution.
rowla said:I'm not saying that C++ is better than Java, but I found pointers very
useful (goto are not good of course
Bryan Castillo said:Daniel Sjöblom <[email protected]_NOSPAM> wrote in message
I have had a few people say this (C is only pass be value) because the
value
you are passing is an adress and you can't change the address. That
argument
doesn't make sense to me. Would you say only that C++ has something
other than pass by value? I think most C++ implementations will still
pass the value as an adrress when passing a reference, so in reality
you are still passing by value.
A real world analogy would be if I had a piece of paper with an address
on it and I copied that address onto another piece of paper that I gave
you. That piece of paper is not the house at that address it is only
a reference to that house. There could of course be several pieces of
paper with that address on it. You could then go to that house and
modify it by painting it blue, but that does not change the piece of
paper I gave you. You could change the address on your paper, but that
does not change the address on my paper. You cannot change my paper,
since we assume that you don't even know where my paper is or really
even the existance of my paper. That is pass by value. I gave you the
value of my paper.
It's a bit harder to come up with a good real world analogy for pass by
reference.
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.