Hal Vaughan wrote:
....
I don't know C or C++, but I've heard of pointers and read comments in
places about Java not having pointers. I'm self taught (other than a VAX
11/780 Assembler class in the 1980s), so I find there's a lot of times
that I know some advanced concepts and miss things that I should know.
Java does have pointers, though they are called "references". It lacks a
lot of dangerous baggage that C added to the concept of "pointer", such
as pointer arithmetic, conversion between pointer and integer types, and
unchecked pointer conversions. Java forces its references to either be
null or point to an object of appropriate class.
Because a lot of programmers learned about pointers in connection with
C, some writers about Java try to avoid the word, on the theory that
anyone learning Java would be too dumb to understand that Java pointers
just point to objects, and don't do anything else.
If I understand all this correctly, then if I want to duplicate a
String[] function, I need to do something like this:
String[] oldCopy = { "Duke", "rulez!" };
String[] newCopy = new String[oldCopy.length];
int i;
for (i = 1; i < oldCopy.length; i++) {
newCopy[i] = oldCopy[i].clone();
}
Is that right? Since I can create a new array that will still reference
the old strings, it seems that the only way to create a complete copy as
it is, as an independent object to save the exact state of oldCopy is to
clone each String individually.
String[] oldCopy = { "Duke", "rulez!" };
String[] newCopy = new String[oldCopy.length];
int i;
for (i = 1; i < oldCopy.length; i++) {
newCopy[i] = new String(oldCopy[i]);
}
but there are VERY few situations in which you need anything like that.
Why do you want to avoid sharing String objects?
Patricia
done.