Does Class casting survive an ObjectStream

G

Gordon Beaton

Lets say I create a Vector that has class cast of holding Strings. I
then store that Vector to disk via ObjectStreams and FileStreams.

When I retrieve the Vector later. Will the Class casting survive the
storage process?

I know I could do an experiment to find this out, but I am also
looking for resoning behind the behaviour of the language.

Thanks for any help you can give in this matter.

Realize that casting doesn't actually change the type of any objects,
so the objects you insert into the vector continue to be whatever they
were before you inserted them.

Your vector will contain the same kinds of objects after
reconstruction as it did before.

/gordon
 
R

Robert W via JavaKB.com

Lets say I create a Vector that has class cast of holding Strings. I then
store that Vector to disk via ObjectStreams and FileStreams.

When I retrieve the Vector later. Will the Class casting survive the storage
process?

I know I could do an experiment to find this out, but I am also looking for
resoning behind the behaviour of the language.

Thanks for any help you can give in this matter.
 
M

Mike Schilling

Robert W via JavaKB.com said:
Lets say I create a Vector that has class cast of holding Strings. I then
store that Vector to disk via ObjectStreams and FileStreams.

When I retrieve the Vector later. Will the Class casting survive the
storage
process?

I know I could do an experiment to find this out, but I am also looking
for
resoning behind the behaviour of the language.

Thanks for any help you can give in this matter.

If the question is "What do I get when I serialize a Vector<String>", the
answer is "A Vector".You can deserialize it back into a Vector,
Vector<Object>, Vector<String>, or even Vector<Integer>, though in the last
case you'll generate a lot of ClassCastExceptions trying to get objects from
it..

This has to do with the notion of erasure: at runtime, the type variables
are erased, and a collection, unlike an array, has no idea what type it was
declared to contain.
 
R

Robert Worrall via JavaKB.com

I am afraid you have answered the wrong question.

I will try to be a little clearer.

I am aware that if I put a Vector in full of strings, i will get back a
Vector full of Strings.

What I am after is: If I try to add an int to this Vector after I have
retrieved it what will happen?
 
M

Mike Schilling

Robert Worrall via JavaKB.com said:
I am afraid you have answered the wrong question.

I will try to be a little clearer.

I am aware that if I put a Vector in full of strings, i will get back a
Vector full of Strings.

What I am after is: If I try to add an int to this Vector after I have
retrieved it what will happen?

That's not it, since you can't add a scalar to a Vector regardless. I will
assume you means "add an Integer". If you've retrieved it into a
Vector<String> you'll get a compilation error. If into a Vector ot
Vector<Object>, it'll work fine. As I said earlier, Collections are unlike
arrays: they don't know what type they're supposed to contain.
 
R

Robert Worrall via JavaKB.com

That's not it, since you can't add a scalar to a Vector regardless.

Ok, That was a silly mistake to make...
I will assume you means "add an Integer". If you've retrieved it into a
Vector<String> you'll get a compilation error. If into a Vector ot
Vector<Object>, it'll work fine. As I said earlier, Collections are unlike
arrays: they don't know what type they're supposed to contain.

This is what I am after. Thanks.
 

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

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top