Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

Does Class casting survive an ObjectStream

Thread view: 
Robert W - 31 Oct 2005 16:24 GMT
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.
Gordon Beaton - 31 Oct 2005 15:59 GMT
> Lets say I create a Vector that has class cast of holding Strings. I
> then store that Vector to disk via ObjectStreams and FileStreams.
[quoted text clipped - 6 lines]
>
> 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

Signature

[  do not email me copies of your followups  ]
g o r d o n + n e w s @  b a l d e r 1 3 . s e

Mike Schilling - 31 Oct 2005 18:14 GMT
> Lets say I create a Vector that has class cast of holding Strings. I then
> store that Vector to disk via ObjectStreams and FileStreams.
[quoted text clipped - 8 lines]
>
> 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.
Robert Worrall - 01 Nov 2005 01:55 GMT
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?
Mike Schilling - 01 Nov 2005 05:23 GMT
>I am afraid you have answered the wrong question.
>
[quoted text clipped - 5 lines]
> 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.
Robert Worrall - 01 Nov 2005 09:44 GMT
>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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.