aspose file tools*
The moose likes Java in General and the fly likes Java 5 Genrics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java 5 Genrics" Watch "Java 5 Genrics" New topic
Author

Java 5 Genrics

Arul Prasad
Ranch Hand

Joined: Jan 20, 2005
Posts: 57
Hi
is there any way to use Genrics in the ObjectOutputStream & ObjectInputSream. in Java 5.

My case wass I'm having a Animal class which need to be written in to one file and read it from the file.

If i am using the ObjectInputStream to read . I need to type cast it explicitly when i am saying

Animal a = (Animal)objectInputStream.readObject();



Can we use Genrics for avoiding this Type cast . Is any thing avail with java 5 if not why it was avoided ...?






With Regards<br />Arul
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14272
    
  21

No, you can't use generics to avoid that cast, because ObjectInputStream is not a generic class.

There's a good reason why it's not a generic class - a stream can contain (and usually does) many different kinds of objects, it wouldn't be very useful to have an ObjectInputStream that could read only one kind of object.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Srinivas Kalvala
Ranch Hand

Joined: Oct 20, 2005
Posts: 257

Hello,

The ObjectInputStream and OutputStream are used to read and write any type of object. When you are trying to readObject from the Stream it will return you the Object type and we have to type cast.

But we can't provide the generic to this one , and the reason is , it reads or writes any type of object. So we dont know which is next object. Just we have to check with type cast and it is safe too.

More inputs are welcome.
Arul Prasad
Ranch Hand

Joined: Jan 20, 2005
Posts: 57
hi Srini

Yes I have also think that reason. but if Collection can use the Genrics . why can't this ObjectOutputStream and ObjectInputStream . Even Collection also can written in to the File. Which can contain the list of many type of Objects.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
I have never tried, so I don't know whether this will work, but:-

Try extending the ObjectInputStream and see whether you can convert it to a generic class.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Arul Prasad:
Yes I have also think that reason. but if Collection can use the Genrics . why can't this ObjectOutputStream and ObjectInputStream .


It could, but it would be of much less value.

When you have a List<Animal>, the compiler makes sure that only Animals are put into the list. So if you get an object from the list, you can be very sure that it *must* be an Animal.

When you have an ObjectInputStream<Animal>, the input is just a binary stream. The compiler can't guarantee that the stream was produced using an ObjectOutputStream<Animal>, so we actually don't know what the stream actually contains.

So, for a list, if you use generics consequently, you can be quite sure that you will never get a ClassCastException - the compiler would already find out about the problem. That's simply not possible for the streams, because you use two different objects to write and read the stream.

Does that sound reasonable?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
 
subject: Java 5 Genrics