J
jimjim
Hello,
I m trying to make my transition from C++ to Java and my curiosity
generated the following question.
The method below aims to return the first element stored in a Vector that
has a particular tag in the Hashtable.
Hashtable tuples = new Hashtable();
private synchronized Object get(String tag)
{
Vector v = (Vector) tuples.get(tag);
//snip
Object o = v.firstElement();
v.removeElementAt(0);
return o;
}
1. The reference to the first element of the Vector is stored in Object o
( = v.firstElement( )). It is a reference that is stored as there is no
"new" statement, isnt it?
2. Latter, what v.removeElementAt(0) does is to copy all Vector's
elements starting from element[1] one place downwards, to element[0], and
delete the element[lastElement]. Should I assume that in this moment, memory
is automatically created in order to store the "actual" (not the reference)
element[0] of the Vector to Object o, before it is overwritten by the
System.copyarray( ) that the Vector uses?
3. In addition to this convenience, should I also assume that even though
the Object o goes out of scope, meaning that the function's stack is
released thus the object is destroyed, it is actually not destroyed as after
returning from the get( ) the object's reference is stored somewhere else in
the program?
Please do correct me if I am wrong, and most importantly share your insight
and knowledge on how things actually work. Responsible answers please. Thank
you in advance.
jimjim
I m trying to make my transition from C++ to Java and my curiosity
generated the following question.
The method below aims to return the first element stored in a Vector that
has a particular tag in the Hashtable.
Hashtable tuples = new Hashtable();
private synchronized Object get(String tag)
{
Vector v = (Vector) tuples.get(tag);
//snip
Object o = v.firstElement();
v.removeElementAt(0);
return o;
}
1. The reference to the first element of the Vector is stored in Object o
( = v.firstElement( )). It is a reference that is stored as there is no
"new" statement, isnt it?
2. Latter, what v.removeElementAt(0) does is to copy all Vector's
elements starting from element[1] one place downwards, to element[0], and
delete the element[lastElement]. Should I assume that in this moment, memory
is automatically created in order to store the "actual" (not the reference)
element[0] of the Vector to Object o, before it is overwritten by the
System.copyarray( ) that the Vector uses?
3. In addition to this convenience, should I also assume that even though
the Object o goes out of scope, meaning that the function's stack is
released thus the object is destroyed, it is actually not destroyed as after
returning from the get( ) the object's reference is stored somewhere else in
the program?
Please do correct me if I am wrong, and most importantly share your insight
and knowledge on how things actually work. Responsible answers please. Thank
you in advance.
jimjim