问题
I want to know the disadvantages of making a class serializable in java;
disadvantages in terms of memory allocation and accessibility.
any link on this topic will be helpful
Thanks in advance.
回答1:
disadvantages in terms of memory allocation
Zero.
and accessibility
Zero. I don't understand the question but the answer is still zero.
If you don't want a specific class member to be serialized, you can always make it transient, which will save any serialization overhead connected with the class that contains that member.
The act of implements Serializable
itself has zero negative consequences.
I don't know what you mean by 'safe', but using Serialization for deep copies certainly works.
回答2:
If you look at Java and its session objects, pure object serialization is used.
Assuming that an application session is fairly short-lived, meaning at most a few hours, object serialization is simple, well supported and built into the Java concept of a session.
However, when the data persistence is over a longer period of time, possibly days or weeks, and you have to worry about new releases of the application, serialization quickly becomes evil.
As any good Java developer knows, if you plan to serialize an object, even in a session, you need a real serialization ID (serialVersionUID), not just a 1L, and you need to implement the Serializable interface.
However, most developers do not know the real rules behind the Java deserialization process.
If your object has changed, more than just adding simple fields to the object, it is possible that Java cannot deserialize the object correctly even if the serialization ID has not changed.
Suddenly, you cannot retrieve your data any longer, which is inherently bad.
Please visit https://softwareengineering.stackexchange.com/questions/191269/java-serialization-advantages-and-disadvantages-use-or-avoid
回答3:
Actually, the only disadvantage i could think of is exposing a pseudo constructor for the object.
For example, if you make a classic singleton serializable, you should also introduce a 'readResolve' method.
Other than that, no disadvantages.
Regarding deep copy, you should use the Clonable facility as it works much faster by using native memcpy.
来源:https://stackoverflow.com/questions/16789076/what-are-the-disadvantages-of-making-a-class-serializable-in-java