Difference between Serializable and Externalizable in Java Serialization
来源:互联网 发布:华三交换机进入端口 编辑:程序博客网 时间:2024/06/05 14:54
1. In case of Serializable, default serialization process is used. while in case of Externalizable custom Serialization process is used which is implemented by application.
2. JVM gives call back to readExternel() and writeExternal() of java.io.Externalizalbe interface for restoring and writing objects into persistence.
3. Externalizable interface provides complete control of serialization process to application.
4. readExternal() and writeExternal() supersede any specific implementation of writeObject and readObject methods.
Though Externalizable provides complete control, it also presents challenges to serialize super type state and take care of default values in case of transient variable and static variables in Java. If used correctly Externalizable interface can improve performance of serialization process.
By implementating java.io.Serializable, you get "automatic" serialization capability for objects of your class. No need to implement any other logic, it'll just work. The Java runtime will use reflection to figure out how to marshal and unmarshal your objects.
In earlier version of Java, reflection was very slow, and so serializaing large object graphs (e.g. in client-server RMI applications) was a bit of a performance problem. To handle this situation, the java.io.Externalizable interface was provided, which is like java.io.Serializable but with custom-written mechanisms to perform the marshalling and unmarshalling functions (you need to implement readExternal and writeExternal methods on your class). This gives you the means to get around the reflection performance bottleneck.
In recent versions of Java (1.3 onwards, certainly) the performance of reflection is vastly better than it used to be, and so this is much less of a problem. I suspect you'd be hard-pressed to get a meaningful benefit from Externalizable with a modern JVM.
Also, the built-in Java serialization mechanism isn't the only one, you can get third-party replacements, such as JBoss Serialization, which is considerably quicker, and is a drop-in replacement for the default.
A big downside of Externalizable is that you have to maintain this logic yourself - if you add, remove or change a field in your class, you have to change your writeExternal/readExternal methods to account for it.
In summary, Externalizable is a relic of the Java 1.1 days. There's really no need for it any more
Read more: http://javarevisited.blogspot.com/2012/01/serializable-externalizable-in-java.html#ixzz2Z6AjeDuf
In earlier version of Java, reflection was very slow, and so serializaing large object graphs (e.g. in client-server RMI applications) was a bit of a performance problem. To handle this situation, the java.io.Externalizable interface was provided, which is like java.io.Serializable but with custom-written mechanisms to perform the marshalling and unmarshalling functions (you need to implement readExternal and writeExternal methods on your class). This gives you the means to get around the reflection performance bottleneck.
In recent versions of Java (1.3 onwards, certainly) the performance of reflection is vastly better than it used to be, and so this is much less of a problem. I suspect you'd be hard-pressed to get a meaningful benefit from Externalizable with a modern JVM.
Also, the built-in Java serialization mechanism isn't the only one, you can get third-party replacements, such as JBoss Serialization, which is considerably quicker, and is a drop-in replacement for the default.
A big downside of Externalizable is that you have to maintain this logic yourself - if you add, remove or change a field in your class, you have to change your writeExternal/readExternal methods to account for it.
In summary, Externalizable is a relic of the Java 1.1 days. There's really no need for it any more
Read more: http://javarevisited.blogspot.com/2012/01/serializable-externalizable-in-java.html#ixzz2Z6AjeDuf
- Difference between Serializable and Externalizable in Java Serialization
- Difference between Externalizable and Serializable in Java
- Difference between Vector and ArrayList in java?
- Difference between Comparator and Comparable in Java
- Difference between TreeMap and TreeSet in Java
- Difference between RegularEnumSet and JumboEnumSet in Java
- Difference between Comparator and Comparable in java
- Difference between Stack and Heap in Java
- Difference between ArrayList and Vector In java
- Difference between HashMap and Hashtable in Java
- Difference between Enumeration and Iterator in Java
- Difference between HashSet and HashMap in Java
- Difference between parseInt and valueOf in java?
- Difference between ArrayList and LinkedList in Java
- Difference between ConcurrentHashMap and Collections.synchronizedMap and Hashtable in Java
- Difference between ConcurrentHashMap and Collections.synchronizedMap and Hashtable in Java
- Java - Difference between Stack and Heap memory in Java
- Java - Difference between Wait and Sleep , Yield in Java
- Linux下禁止root用户远程登录的方法
- Suse Linux 多次登录失败锁定用户及解锁
- Java反射效率
- java反射机制基础详解(转)
- transient, volatile, strictfp
- Difference between Serializable and Externalizable in Java Serialization
- C++箴言:绝不在构造或析构期调用虚函数
- Java多态特性
- web程序请求限制
- Collection类区别
- How to disable certain HTTP methods (PUT, DELETE, TRACE and OPTIONS) in JBOSS7
- POJ-2492-A Bug's Life
- 在eclipse上安装 Marketplace Client
- Java使用qqwry.dat根据IP地质获取城市信息