Getting An Object From A Vector Without Casting

H

Hal Vaughan

I'll start by saying I've always had a bit of trouble with OOP and keeping
clear when, in a language like Java, I'm working with a pointer or a new
instance of an object.

As best I can tell, if I have the following:

MyObject myObj = new MyObject;
Vector myVec = new Vector();

myVec.add(myObj);
..
..
..
//here i points to the object we're using
MyObject newObj = (MyObject) myVec.get(i);

It seems that newObj is not the same instance of myObj but a copy of it that
I can alter without effecting myObj. Is that right? (I could have
misunderstood the results when I ran a test.)

If that is right, that by casting, I'm creating a new object, and, as best I
can tell, anything in a Vector is treated as Object and not a type of
object, how can I get an object out of a Vector (or LinkedList or HashMap)
either without casting or by making sure that the resulting object is just
another instance of the one in the Vector?

Thanks!

Hal
 
H

Hal Vaughan

yogi said:
use generics feature of java 5...

Thanks.

Any way to do it in Java 2? The installer I'm using (a FOSS project) has a
couple issues with Java 5 and there's a few other issues, so I need to
stick with Java 2 for compatibility.

Hal
 
M

Matt Humphrey

Hal Vaughan said:
I'll start by saying I've always had a bit of trouble with OOP and keeping
clear when, in a language like Java, I'm working with a pointer or a new
instance of an object.

As best I can tell, if I have the following:

MyObject myObj = new MyObject;
Vector myVec = new Vector();

myVec.add(myObj);
.
.
.
//here i points to the object we're using
MyObject newObj = (MyObject) myVec.get(i);

It seems that newObj is not the same instance of myObj but a copy of it
that
I can alter without effecting myObj. Is that right? (I could have
misunderstood the results when I ran a test.)

No. myObj is a variable whose contents is the reference to your first object
instance.

myVec.add (myObj) copies that reference (but not the object) to the vector.
There is only one copy of the object and it is floating out in the heap
somewhere. There are two references to it. One is in the variable myObj
and the other is somewhere in myVec.
MyObject newObj = (MyObject) myVec.get(i);

Presuming that i is the correct location of your object (0 if there's
nothing else there), this statement copies the reference into newObj. There
are now three copies of the reference and they all point to the one object
instance.
If that is right, that by casting, I'm creating a new object, and, as best
I
can tell, anything in a Vector is treated as Object and not a type of
object, how can I get an object out of a Vector (or LinkedList or HashMap)
either without casting or by making sure that the resulting object is just
another instance of the one in the Vector?

As you should be able to tell now, assignment (and parameter passing) copies
the reference to the object but not the object itself. Casting does nothing
at all--it is simply a hint you give to the compiler to assert that class of
the object coming from the expression will be of the named type.

Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
O

Oliver Wong

Hal Vaughan said:
Thanks.

Any way to do it in Java 2? The installer I'm using (a FOSS project) has
a
couple issues with Java 5 and there's a few other issues, so I need to
stick with Java 2 for compatibility.

Run "java -version" and tell us what the output is. I suspect there's a
misunderstanding about what version you're actually running. The
misunderstanding is not your fault; Sun came up with a *TERRIBLE* numbering
scheme for their Java product. For example, my output is:

java version "1.6.0-beta2"
Java(TM) SE Runtime Environment (build 1.6.0-beta2-b86)
Java HotSpot(TM) Client VM (build 1.6.0-beta2-b86, mixed mode, sharing)

And it's a *beta* version of 1.6, because 1.6 is not even out yet.

- Oliver
 
I

Ian Shef

I will intersperse your code with my description of what is going on.
Perhaps it will help.

I'll start by saying I've always had a bit of trouble with OOP and
keeping clear when, in a language like Java, I'm working with a pointer
or a new instance of an object.

As best I can tell, if I have the following:

MyObject myObj = new MyObject;

First, you left out a pair of parentheses.

"MyObject myObj" defines myObj as an object reference that will reference
(point at) only instances of the class MyObject.

"new MyObject()" creates an instance of the class, in part by calling the no-
argument constructor.

Due to the "=", myObj now holds a reference (points at) an instance created
by "new MyObject". We tend to speak informally and say that myObj is the
object created by "new MyObject()", but in fact it is only a reference
(pointer) to it.
Vector myVec = new Vector();

myVec.add(myObj);

myVec now holds a reference (points at) the same instance created previously.
.
.
.
//here i points to the object we're using
MyObject newObj = (MyObject) myVec.get(i);

"MyObject newObj" defines newObj as an object reference that will reference
(point at) only instances of the class MyObject.

Assuming that "i" has an appropriate value, "myVec.get(i)" returns a value
which is a reference (of class Object) to the _same_ instance created
previously.

The "(MyObject)" cast verifies that the reference is actually to an object of
class MyObject, and provides a reference (of class MyObject) to the _same_
instance created previously.

Due to the "=", newObj now holds a reference (points at) an instance created
previously.
It seems that newObj is not the same instance of myObj but a copy of it
that I can alter without effecting myObj. Is that right? (I could have
misunderstood the results when I ran a test.)

I won't say that you misunderstood; it must have been a poor test. :)

Vector does NOT create a new instance. Casting does NOT create a new
instance.
If that is right, that by casting, I'm creating a new object, and, as
best I can tell, anything in a Vector is treated as Object and not a
type of object, how can I get an object out of a Vector (or LinkedList
or HashMap) either without casting or by making sure that the resulting
object is just another instance of the one in the Vector?

In this example, you never creaated more than one instance of class MyObject.
You created three references (myObj, newObj, and the one stored in myVec) all
to the same instance. This is sometimes called aliasing, and is a natural
way that Java operates.

If you change some property of the instance, say with:

myObj.changeProperty() ;

you would see the property change on myObj, newObj, and myVec.get(i) because
they are all referencing the same instance.

Try this (not guaranteed to compile):

StringBuilder sb1 = new StringBuilder("abc") ;
Vector vec = new Vector() ;
vec.add(sb1) ;
StringBuilder sb2 = (StringBuilder)vec.get(0) ;

sb1.append("xyz") ;

System.out.println(sb1);
System.out.println(vec.get(0));
System.out.println(sb2);
Thanks!

Hal

Best wishes!
 
O

Oliver Wong

Hal Vaughan said:
I'll start by saying I've always had a bit of trouble with OOP and keeping
clear when, in a language like Java, I'm working with a pointer or a new
instance of an object.

As best I can tell, if I have the following:

MyObject myObj = new MyObject;
Vector myVec = new Vector();

myVec.add(myObj);
.
.
.
//here i points to the object we're using
MyObject newObj = (MyObject) myVec.get(i);

It seems that newObj is not the same instance of myObj but a copy of it
that
I can alter without effecting myObj. Is that right? (I could have
misunderstood the results when I ran a test.)

It's wrong. Exactly two objects are created in the code you've shown
above: An instance of MyObject and an instance of Vector. When you put
MyObject into the vector, and get it back out, you've working with the same
instance of the object.

Incidentally, I'd avoid phrasing it as "i points to the object we're
using", as that implies that i is a pointer or a reference. Instead, i is
(probably) an int whose value is equal to the index of the object's position
in the vector. Yes, it's a bit verbose, but since there seems to be
conceptual problems about how objects and references and all that works in
Java, I think it's important in this thread to be very careful about
terminology.

- Oliver
 
H

Hal Vaughan

Oliver said:
Run "java -version" and tell us what the output is. I suspect there's
a

It's 1.4.2. Full output:

java version "1.4.2_02"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_02-b03)
Java HotSpot(TM) Client VM (build 1.4.2_02-b03, mixed mode)

Hal
 
O

Oliver Wong

Hal Vaughan said:
It's 1.4.2. Full output:

java version "1.4.2_02"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_02-b03)
Java HotSpot(TM) Client VM (build 1.4.2_02-b03, mixed mode)

Okay, using *THIS* numbering system, generics was added in 1.5, and
there's no way to do it in 1.4.2. You'd have to do the casting.

- Oliver
 
H

Hal Vaughan

Oliver said:
It's wrong. Exactly two objects are created in the code you've shown
above: An instance of MyObject and an instance of Vector. When you put
MyObject into the vector, and get it back out, you've working with the
same instance of the object.

Incidentally, I'd avoid phrasing it as "i points to the object we're
using", as that implies that i is a pointer or a reference. Instead, i is
(probably) an int whose value is equal to the index of the object's
position in the vector. Yes, it's a bit verbose, but since there seems to
be conceptual problems about how objects and references and all that works
in Java, I think it's important in this thread to be very careful about
terminology.

It may be verbose, but it does help in keeping things straight. I used to
preface most of my questions by pointing out I'm self taught. I used to
program on an Apple //e back in the 1980s, then a few years ago had to
teach myself Perl, Java, and a few other languages. That means that I've
had to work from a practical understanding and don't have the benefit of
many of the seemingly pedantic points that you get in a classroom that
later make sense. For me, dealing with pointers, instances, and OOp, have
been the hardest things for me to get used to. While I've produced some
quite useful code, I still get tripped up on these things and need to check
and see if my understanding is on or off target.

Thanks!

Hal
 
H

Hal Vaughan

Sorry for the top post, but this is quick and simple and I didn't want to
cut your explanation so it's there when people search and find this.

I don't need to try to compile your example. Your comments have helped me a
*lot* in understanding this and not just answering the immediate question,
but giving me a clearer understanding overall.

Thanks!

Hal
 
O

Oliver Wong

myVec now holds a reference (points at) the same instance created
previously.

To clarify, myVec actually points at the instance of the Vector that was
created with the expression "new Vector()". Then, when myVec.add() was
invoked, the reference myObj is passed. Internally, vector basically makes a
copy of the *REFERENCE*, storing it somewhere. That reference still points
to the same instance of MyObject. In other words, the three references
"myObj", "newObj", and the reference that's part of the implementation of
Vector, are all pointing to the same object.

- Oliver
 
L

Lasse Reichstein Nielsen

Hal Vaughan said:
I'll start by saying I've always had a bit of trouble with OOP and keeping
clear when, in a language like Java, I'm working with a pointer or a new
instance of an object.

That's fairly simple. If you don't use the "new" operator, you don't
create a new object [1].
As best I can tell, if I have the following:

MyObject myObj = new MyObject;
Vector myVec = new Vector();

myVec.add(myObj);
.
.
.
//here i points to the object we're using
MyObject newObj = (MyObject) myVec.get(i);

It seems that newObj is not the same instance of myObj but a copy of it that
I can alter without effecting myObj. Is that right?

No. The object you get out of the vector is the same object you put in.
(I could have misunderstood the results when I ran a test.)

You probably did.
If that is right, that by casting, I'm creating a new object,

Definitly not. Casting does nothing except test that the cast is
allowed and then allow you to treat the object as the type cast to.
and, as best I can tell, anything in a Vector is treated as Object
and not a type of object,

Without generics, the return type of "get(int)" is Object, so all you
know about the object you get out is that it's an Object. If you know
that it is in fact something else, and you want to be able to treat
it as such, you will have to do a cast.
how can I get an object out of a Vector (or LinkedList or HashMap)
either without casting or by making sure that the resulting object is just
another instance of the one in the Vector?

Not "another instance", but "the same instance".

If you want to avoid the cast, you should use a generic collection:

Vector<MyObject> myVec = new Vector<MyObject>;
...
MyObject newObj = myVec.get(i);


/L
 

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,778
Messages
2,569,605
Members
45,238
Latest member
Top CryptoPodcasts

Latest Threads

Top