Learn Java Tutorial for Beginners, Part 50: Serialization

Pubblicato il: 17 ottobre 2020
sul canale di: Quordnet Academy
90
4

In this video by Quordnet Academy for the series Learn Java Tutorial for Beginners how to use Serialization have been discussed.
If you would like to discover even more about java programming tutorial or the java complete course I advise you to check out the playlist :
   • Java Tutorial For Beginners  

Serialization and Deserialization in Java
Serialization
Serializable Interface
Example of Serialization
Example of Deserialization
Serialization with Inheritance
Externalizable interface
Serialization and static data member
Serialization in Java is a mechanism of writing the state of an object into a byte-stream. It is mainly used in Hibernate, RMI, JPA, EJB and JMS technologies.

The reverse operation of serialization is called deserialization where byte-stream is converted into an object. The serialization and deserialization process is platform-independent, it means you can serialize an object in a platform and deserialize in different platform.

For serializing the object, we call the writeObject() method ObjectOutputStream, and for deserialization we call the readObject() method of ObjectInputStream class.


We must have to implement the Serializable interface for serializing the object.

Advantages of Java Serialization
It is mainly used to travel object's state on the network (which is known as marshaling).

java serialization
java.io.Serializable interface
Serializable is a marker interface (has no data member and method). It is used to "mark" Java classes so that the objects of these classes may get a certain capability. The Cloneable and Remote are also marker interfaces.

It must be implemented by the class whose object you want to persist.

The String class and all the wrapper classes implement the java.io.Serializable interface by default.

Let's see the example given below:

import java.io.Serializable;
public class Student implements Serializable{
int id;
String name;
public Student(int id, String name) {
this.id = id;
this.name = name;
}
}
In the above example, Student class implements Serializable interface. Now its objects can be converted into stream.



ObjectOutputStream class
The ObjectOutputStream class is used to write primitive data types, and Java objects to an OutputStream. Only objects that support the java.io.Serializable interface can be written to streams.

Constructor
1) public ObjectOutputStream(OutputStream out) throws IOException {} creates an ObjectOutputStream that writes to the specified OutputStream.
Important Methods
Method Description
1) public final void writeObject(Object obj) throws IOException {} writes the specified object to the ObjectOutputStream.
2) public void flush() throws IOException {} flushes the current output stream.
3) public void close() throws IOException {} closes the current output stream.
ObjectInputStream class
An ObjectInputStream deserializes objects and primitive data written using an ObjectOutputStream.

Constructor
1) public ObjectInputStream(InputStream in) throws IOException {} creates an ObjectInputStream that reads from the specified InputStream.
Important Methods
Method Description
1) public final Object readObject() throws IOException, ClassNotFoundException{} reads an object from the input stream.
2) public void close() throws IOException {} closes ObjectInputStream.
Example of Java Serialization
In this example, we are going to serialize the object of Student class. The writeObject() method of ObjectOutputStream class provides the functionality to serialize the object. We are saving the state of the object in the file named f.txt.

Possibly if you have doubt comment below and let me understand what else I can help you with information in java.This playlist is a full fledged java programming for beginners and java tutorial for beginners which is given above.
Please share with your friends the video to assist other people looking for java programming tutorial or object oriented programming java .
To never miss an update from or channel hit the subscribe button first and if already subscribe hit the bell icon.
1.Follow us on INSTAGRAM for Interesting posts
  / quordnet_academy  
2.Follow us on LINKEDIN for interesting content on different aspects
  / quordnet-academy  
3.Don't forget to like our FACEBOOK to get the most out of it
  / quordnetacademy  
4.Follow us on twitter to get a mix of all
  / quordnetacademy  
5.If you want to get us on TUMBLR please then click on the link given below
https://www.tumblr.com/blog/quordnet-...
6.Do join our OFFICIAL Telegram for notes of different things
https://t.me/quordnet
7.For get job update regularly both private and government do join this telegram channel
https://t.me/quordnetforjobs
#quordnetacademy, #java_tutorial_series, #javatutorialseries


In questa pagina del sito puoi guardare il video online Learn Java Tutorial for Beginners, Part 50: Serialization della durata di ore minuti seconda in buona qualità , che l'utente ha caricato Quordnet Academy 17 ottobre 2020, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 90 volte e gli è piaciuto 4 spettatori. Buona visione!