• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

ob. serialization  RSS feed

 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi!
here is a ? on Object Serialization. Can we get question of this type in exam? 'coz interfaces like Externalizable and Serializable haven't been given in exam objectives.
thanx.
ashok.
_________
Question 80. (from <http://www.angelfire.com/or/abhilash/Main.html> )
import java.io.*;
public class TransientWriter implements Externalizable {
private transient String s = "Hope I can ever be persistant!";

public void writeExternal(ObjectOutput oOut) throws IOException
{
oOut.writeObject(s);
}

public void readExternal(ObjectInput oIn) throws IOException, ClassNotFoundException
{
s=(String)oIn.readObject();
}
public String toString()
{
return s;
}
}
class K {
public static void main(String args[]) throws IOException, ClassNotFoundException {
TransientWriter tw = new TransientWriter();
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("tw.out"));
out.writeObject(tw);
ObjectInputStream in = new ObjectInputStream(new FileInputStream("tw.out"));
TransientWriter tw2 = (TransientWriter) in.readObject();
System.out.println(tw2);
}
}
Attempting to compile and run the above code

1) will cause a compiler error due to the attempt to write a transient object.
2) will cause a runtime exception when an attempt is made to write a transient object.
3) will not cause any runtime error and the transient object is writen to the file named "tw.out".
4) will not cause any runtime error and the transient object is not written to the file named "tw.out". The program prints a blank line on the screen.
 
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmmm... i really think tht we CAN get this type of q in exam , coz it seem to test our knowlegde about Object Streams , n transient keyword
( guess if serializable n externlizable are nt expicitly mentioned , still Object stream include them implicitly , no? LOL )
Just try to remember these things , Object streams can "serialize" n thus write-read only those objects which either implement :
-serializable interface ( Contanins no-method )
-or externlizable interface ( contanis only 2 methods wid sig :
public void writeExternal(ObjectOutput oOut) n public void readExternal(ObjectInput oIn)
And memebers wid modifier "transient" are not serialized even if the object implements serilizable
( the above code-ex serialize transient-memebers by explicitly implementing externalizable's method in such a way , externalizable is meant for this purpose , to overrule the default-functionality provided by seralizable )
this shld be enough i guess
------------------
Gagan (/^_^\)
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! I tried running the code but it gives following exception.

java.io.InvalidClassException: TransientWriter; IllegalAccessException
at java.io.ObjectInputStream.inputObject(ObjectInputStream.java:1224)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:386)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:236)
at K.main(K.java:32)
Exception in thread "main"
Tool completed with exit code 1
In tw.out the transient String is written, the contents of tw.out are ->
�� sr TransientWriter�`Z��X�� xpt Hope I can ever be persistant!x
Why am i getting the exception?


Originally posted by Gagan Indus:
hmmm... i really think tht we CAN get this type of q in exam , coz it seem to test our knowlegde about Object Streams , n transient keyword
( guess if serializable n externlizable are nt [b] expicitly
mentioned , still Object stream include them implicitly , no? LOL )
Just try to remember these things , Object streams can "serialize" n thus write-read only those objects which either implement :
-serializable interface ( Contanins no-method )
-or externlizable interface ( contanis only 2 methods wid sig :
public void writeExternal(ObjectOutput oOut) n public void readExternal(ObjectInput oIn)
And memebers wid modifier "transient" are not serialized even if the object implements serilizable
( the above code-ex serialize transient-memebers by explicitly implementing externalizable's method in such a way , externalizable is meant for this purpose , to overrule the default-functionality provided by seralizable )
this shld be enough i guess
[/B]


 
Gagan Indus
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vipin ,
try this :
Type foll code in a filename called TransientWriter.java n compile it


Type foll code in a filename called K.java n compile n run it


------------------
Gagan (/^_^\)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!