Regarding the topic serialization:
In a normal Java program when you design a class and instantiate its object, the object is in RAM(JVM) and it is treated as instructions.
When you want to serialize it (ie convert it to character stream and transfer it to a file or network), you want to tell java to see it as data and not instruction now.
Am I getting it right?
I am a CS major student so I want to look it things that way, maybe I'm very incorrect, please correct me.
#Understanding Serialization in details
1 messages ยท Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
Here is an AI assisted attempt to answer your question ๐ค. Maybe it helps! In any case, a human is on the way ๐. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
Regarding the topic serialization:
In a normal Java program when you design a class and instantiate its object, the object is in RAM(JVM) and it is treated as instructions.
When you want to serialize it (ie convert it to character stream and transfer it
Serialization in Java is the process of converting an object into a byte stream, which can be saved to a file, sent over a network, or stored in a database. This allows the object's state to be saved and recreated later. To serialize an object, it must implement the Serializable interface.
Some key points to understand about serialization:
- Serialized objects can be deserialized back into their original form.
- Not all objects can be serialized, as transient fields and static fields are not serialized by default.
- Custom serialization can be implemented by providing readObject() and writeObject() methods in the class.
Useful links:
- AI generated response
The class live in ram as data, when you serialize it, you first need to decide the format, then your goal it to transform your object in that format and back.
it might help to make a simple example
suppose u have an instance of a class like this one:
Person p = new Person("John", 20);
The object is data, not instructions
great, in ur RAM u now have a java object floating around with name John and age 20
but what if u want to persist it, maybe as a file
so u decide to write some code like maybe:
String text = "name: " + p.getName() + ", age: " + p.getAge();
Files.write(Path.of("person.txt"), text);
now u got a file person.txt with content
name: John, age: 20
great, u just serialized ur java object
now its content is in a file
easy, there is no magic to the idea of serialization
next u might want to be able to also read that file back into an object
so u would write some javacode that reads this file and recreates a person instance from its contents
String text = Files.read(Path.of("person.txt"));
String[] parts = text.split(", ");
String name = parts[0].split("name: ")[1];
String age = Integer.parseInt(parts[1].split("age: ")[1]);
Person p = new Person(name, age);
great, now uve got ur john back as java object
going beyond this simple example the main choice is which data format u want to pick for ur serialization
in this case we made a simple text file with a custom format
in practice u would typically choose something more widespread
CSV:
john;20
JSON:
{
"name": "John",
"age": 20
}
maybe XML:
<person>
<name>John</name>
<age>20</age>
</person>
maybe some binary formats like javas built-in serialization
or protobuf
or bson
there are many ways to go forward
but the idea is always the same
๐
and applications also go beyond persisting ur data as file
for example if u want to send data from one computer to another through "the internet"
then we often do that with HTTP, for example in rest-apis
so u need to make ur java object into something that can be send around and understood by other systems as well
(most common choice for that use case is JSON)
one more thing, suppose I've serialized an object to this pojo.yml or pojo.xml or whatever format file, and now I've added this file in assets/ of another project's repo (local) and I've deserialized it too, but what about static methods and fields?
and before that question, the jvm has to load the class itself, right? for various decisions like static analysis, etc etc.
Anything static is ignored as it is not in the class instance