Chris Smith said:
Bryan said:
I disagree with the statement from
Chris that java can not modify the value actually passed to it.
Consider the following code:
[...]
Do you have any indication of whether or not the ArrayList will be
modified? Here is the output of me running this program.
You're missing the point. The ArrayList object is not the parameter.
Because it's an object, it can never be a parameter in Java. Yes, you
can use the parameter (which is a reference) to get to some common
shared data, but you still can't modify the parameter.
That may sound picky... but we are talking about language semantics,
after all. It's also important, though, because when I pass an object
that can only be modified through methods, I can know precisely what I
do and don't expect to occur with that object, because it's documented
in the API of the object itself. C++, on the other hand, provides a
single const modifier (which is very helpful and covers most common
cases), but otherwise allows any change to a pass-by-reference
parameter.
Its not just picky, its a technicality. To me the ArrayList is the
parameter. Yes you can seperate the concept of an object and its
reference but can you actually use an object without its reference in
java? There is only one way to access the Object and that is through
its reference, to me the reference is the Object. When we start
saying things like "no you aren't really passing something that can be
modified because the thing your passing isn't really the thing your
interested in, its the indirect reference to it", we end up confusing
people.
If we get picky, you can't really modify the parameter passed in C++
either, because you are really only passing the Address of something
not the thing itself. But you will say "Wait, in C++ we have
references thats different". Not really, it will probably be
implemented the same as though you passed an address. To illustrate
this point. Here are 2 C++ files that I output the assembly for. The
assembly is exactly the same, except for a directive saying the name
of the source file.
tuxedo@jabba:/tuxedo/bryanc/testc>cat test1.cpp
extern "C" void modify (int * i) {
*i=0;
}
tuxedo@jabba:/tuxedo/bryanc/testc>cat test2.cpp
extern "C" void modify (int & i) {
i=0;
}
tuxedo@jabba:/tuxedo/bryanc/testc>gcc -S -c *.cpp
tuxedo@jabba:/tuxedo/bryanc/testc>diff test1.s test2.s
1c1
< .file "test1.cpp"
---
Ok, now you can say but it is different because of language semantics.
Well the language semantics of java seem exactly the same to me as
passing a reference in C++. If you say that you aren't really passing
some that can be modified in java because its a reference than you
should also say the same about references in C++.