Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Donnie Turner
2,063 PointsCan someone please help clarify exactly why Serialization is necessary and how it is implemented in Java.
I am currently working in "Java Data Structures" with Craig Dennis and would appreciate a little more clarity on Serialization and its purpose. I read that it is a major security issue with regard to the files it creates.
3 Answers
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 PointsWell yes and no, I like more this quote from http://www.tutorialspoint.com/java/java_serialization.htm:
"Java provides a mechanism, called object serialization where an object can be represented as a sequence of bytes that includes the object's data as well as information about the object's type and the types of data stored in the object.
After a serialized object has been written into a file, it can be read from the file and deserialized that is, the type information and bytes that represent the object and its data can be used to recreate the object in memory."
So coming back to your definition: Serialization is the process of encoding information turning it into sequence of bytes to a file, that can be De-serialized or decoded from the file faster.
About Key: Quote from docs:
"The serialization runtime associates with each serializable class a version number, called a serialVersionUID, which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization. If the receiver has loaded a class for the object that has a different serialVersionUID than that of the corresponding sender's class, then deserialization will result in an InvalidClassException. A serializable class can declare its own serialVersionUID explicitly by declaring a field named "serialVersionUID" that must be static, final, and of type long:
ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L;
If a serializable class does not explicitly declare a serialVersionUID, then the serialization runtime will calculate a default serialVersionUID value for that class based on various aspects of the class, as described in the Java(TM) Object Serialization Specification. However, it is strongly recommended that all serializable classes explicitly declare serialVersionUID values, since the default serialVersionUID computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected InvalidClassExceptions during deserialization. Therefore, to guarantee a consistent serialVersionUID value across different java compiler implementations, a serializable class must declare an explicit serialVersionUID value. It is also strongly advised that explicit serialVersionUID declarations use the private modifier where possible, since such declarations apply only to the immediately declaring class--serialVersionUID fields are not useful as inherited members. Array classes cannot declare an explicit serialVersionUID, so they always have the default computed value, but the requirement for matching serialVersionUID values is waived for array classes."
So as you see, key is only part of the process of the serialization. You may even skip specifiying it, although it is advisable from the docs. This key is needed for JVM to work with. You are just a spectator. You just make class Serializable, generate key, see here, e.g:
https://www.mkyong.com/java/how-to-generate-serialversionuid/
And with that you can use the same objects, from file that you saved to, very fast and efficient.
I tried my best, I'm not Java Developer, just a student, so try to dig deeper into the internet and docs to find your explanation :)
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 PointsThere is a ton of resources out there: start from Stack and tutorials point. They have very good description:) :
http://stackoverflow.com/questions/2232759/what-is-the-purpose-of-serialization-in-java
Donnie Turner
2,063 PointsLet me see if I can sum up what I am reading. Serialization is the process of encoding information to a file using a serialization key, that can only be De-serialized or decoded from the file using the same serialization key that it was created with. Is this correct?
Donnie Turner
2,063 PointsDonnie Turner
2,063 PointsAlexander,
Thank you for you time and explanation. Very useful information. I am trying to get caught up on coding after about 3 1/2 years of no coding time. Java is new to me, but I am sure with help from people like you, I will get it down.