• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Serialization Question

 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
source: http://www.examulator.com/phezam/showquestion.php

import java.io.*;

public class Serialize1{
public static void main(String argv[]) throws IOException{
Serialize1 o = new Serialize1();
o.go();
}
public void go() throws IOException{
String[] obs = {"one","two","three"};
FileOutputStream out = new FileOutputStream("mystrings.ob");
ObjectOutputStream s = new ObjectOutputStream(out);
s.writeObject(obs);
s.flush();
}

}

Answer is : Compilation and output of the serialized content of the obs String array to a new file called mystrings.ob

My Doubt is why it is not raising any exception or giving any error when the class is not implementing Serialize interface?

Thanks,
Geeta Vemula


 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Geeta, you are serializing object obs which is an array of Strings and not Object ob which is an instance of Serialize1



and since String class implements Serializable interface the answer is correct.
 
geeta vemula
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then does arrays implement serializable interface by default?
I am not clear with this.

Thanks,
geeta Vemula
 
Harvinder Thakur
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It has got nothing to do with array being serializable or not. An array of a class is serializable if all the contents of the array are serailizable objects.

When i say String imlements serializable then i am referring to the contents of the String array. All the objects in the array are Strings and String implements serializable.

Look at the following code. Even though Object class does not implement serializable i am still able to serialize an Object array containing all String objects which are serializable.



Hope this clears your doubt.
 
Slime does not pay. Always keep your tiny ad dry.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic