This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

After serialize a object what will be the value of reference object and primitive  RSS feed

 
Dhineshkumar raj
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After serialize a particular object what will the value its object references and primitives ???

please go through the following scenario

import java.io.*;

class Dog implements Serializable{
String dogString = "haiHai";
int dogSize = 555;
public String toString(){
return "This is Object Man";
}
}

class MyTestSerial{
Dog d = new Dog();
MyTestSerial(){
try{
System.out.println("Before Serialization Dog object is : " +d);
System.out.println("Before Serialization d.dogSize : " +d.dogSize);
FileOutputStream fis = new FileOutputStream("MyTest.ser");
ObjectOutputStream obs = new ObjectOutputStream(fis);
obs.writeObject(d);
obs.writeInt(d.dogSize);
obs.writeObject(d.dogString);
System.out.println("After Serialization Dog object is : " +d);
System.out.println("Before Serialization d.dogSize : " +d.dogSize);
}catch(Exception e){
System.out.println(e);
}
}
public static void main(String[] args) {
MyTestSerial t = new MyTestSerial();
}
}

In the above MyTestSerial class am serializing Dog object. After executing this obs.writeObject(d); the dog object will serialize without any problem.
Dog object also having String dogString and int dogSize
Now, once i try to print the dog object and dogString it should print the null value right ??
and its primitive dogSize also print the value zero right??

But am not getting these results. What is my mistake here???

Generally What will happen when we serialize a object or primitive value. After this what is the value of these types??

Can any one a give the clear solution???

Regards
Dhinesh Kumar R
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dhinesh:
Now, once i try to print the dog object and dogString it should print the null value right ??
and its primitive dogSize also print the value zero right??

No, Serialization does not make any changes to the object that is serialized. This means that the state of the object before and after serialization does not change.
Serialization is meant to persist the state of the object so that it can be retrieved later or in a different JVM. It does not alter the state of the object that is serialized.
As an analogy, you can think about your dataobjects that are persisted to a database. If you persist a dataobject to the database, the dataobject itself does not change, correct? Same is with serialization.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!