Pengertian menurut wikipedia :
serialization is the process of converting an object into a sequence of bits so that it can be persisted on a storage medium (such as a file, or a memory buffer) or transmitted across a network connection link to be “resurrected” later in the same or another computer environment. When the resulting series of bits is reread according to the serialization format, it can be used to create a semantically identical clone of the original object. For many complex objects, such as those that make extensive use of references, this process is not straightforward
selanjutnya di bagian java :
Java provides automatic serialization which requires that the object be marked by implementing the java.io.Serializable interface. Implementing the interface marks the class as “okay to serialize,” and Java then handles serialization internally. There are no serialization methods defined on the Serializable interface, but a serializable class can optionally define methods with certain special names and signatures that if defined, will be called as part of the serialization/deserialization process. The language also allows the developer to override the serialization process more thoroughly by implementing another interface, the Externalizable interface, which includes two special methods that are used to save and restore the object’s state.
- There are three primary reasons why objects are not serializable by default and must implement the Serializable interface to access Java’s serialization mechanism. Not all objects capture useful semantics in a serialized state. For example, a Thread object is tied to the state of the current JVM. There is no context in which a deserialized Thread object would maintain useful semantics.
- The serialized state of an object forms part of its class’s compatibility contract. Maintaining compatibility between versions of serializable classes requires additional effort and consideration. Therefore, making a class serializable needs to be a deliberate design decision and not a default condition.
- Serialization allows access to non-transient private members of a class that are not otherwise accessible. Classes containing sensitive information (for example, a password) should not be serializable or externalizable.
The standard encoding method uses a simple translation of the fields into a byte stream. Primitives as well as non-transient, non-static referenced objects are encoded into the stream. Each object that is referenced by the serialized object and not marked as transient must also be serialized; and if any object in the complete graph of non-transient object references is not serializable, then serialization will fail. The developer can influence this behavior by marking objects as transient, or by redefining the serialization for an object so that some portion of the reference graph is truncated and not serialized. It is possible to serialize Java objects through JDBC and store them into a database. While Swing components do implement the Serializable interface, they are not portable between different versions of the Java Virtual Machine. As such, a Swing component, or any component which inherits it, may be serialized to an array of bytes, but it is not guaranteed that this storage will be readable on another machine.
Kira2 intinya serialization itu suatu mekanisme atau proses menyimpan/mengubah suatu object ke rangkaian byte-byte agar bisa disimpan ke dalam media penyimpanan seperti memory atau dalam bentuk file. Deretan byte tersebut dapat kita simpan menjadi suatu file atau dalam database atau ditransfer lewat jaringan. Proses untuk mendapatkan object dari sequence of bytes disebut proses deserialization. Bila object yang serializable memiliki object yang tidak
serializable, maka objek tersebut harus ditandai dengan deklarasi “transient”.
Kalo di java serialisasi ditandai dengan mengimplementasikan interface Serializable yang ada di paket java.io.Serializable. Sebenernya kebanyakan object di Java API udah mengimplementasikan interface ini, misalnya AWT, Swing, String, Arrays.
Trus kalo gitu kapan kita pake interface Serializable dan kenapa di java ga semua class secara default meng-implementasikan interface Serializable?
Berdasarkan kutipan diatas ada tiga alasan utama kenapa objek tidak Serializable secara default dan harus meng-implementasikan interface Serializable untuk mengakses serialisasi Jawa. Pertama ada objek yang tidak membutuhkan serialisasi, misalkan Thread, Socket, OutputStream dan subclass-nya. Untuk apa menyimpan thread yang berjalan terus-menerus dan memakai memory komputer. Kedua membuat serialisasi suatu objek merupakan bagian kompatibilitas kelas. Menjaga kompatibilitas antara versi kelas Serializable memerlukan usaha tambahan dan pertimbangan karena bukan untuk kondisi default. Ketiga Serialisasi memungkinkan akses ke kelas yang berisi informasi sensitif misalnya password, so tidak boleh dilakukan Serializable atau externalizable.
Membuat object menjadi serializable :
import java.io.Serializable;
import java.util.Date;
import java.util.Calendar;
public class PersistentTime implements Serializable {
private Date time;
public PersistentTime() {
time = Calendar.getInstance().getTime();
}
public Date getTime() {
return time;
}
}
Menyimpan object menjadi suatu file :
import java.io.FileOutputStream;
import java.io.IOException;
public class FlattenTime {
public static void main(String [] args) {
PersistentTime time = new PersistentTime();
FileOutputStream fos = null;
ObjectOutputStream out = null;
try {
fos = new FileOutputStream("time.obj");
out = new ObjectOutputStream(fos);
out.writeObject(time);
out.close();
} catch(IOException ex) {
ex.printStackTrace();
}
}
}
Ada 2 aturan mengenai Serializable Objek :
- The object to be persisted must implement the Serializable interface or inherit that implementation from its object hierarchy
- The object to be persisted must mark all nonserializable fields transient
Untuk lebih jelas nya silahkan baca di Wikipedia or ada juga sample n penjelasannya dari Sun
Posted by yanuarseno
Posted by yanuarseno
Posted by yanuarseno