What Is Serialization In Java?

What Is Serialization In Java?

What Is Serialization In Java?


No alt text provided for this image


Serialization in Java converts an object’s state into a byte stream; deserialization is the reverse process. Another way, serialization is converting a Java object into a static stream (sequence) of bytes that we can then preserve in a database or send over the internet. Deserialization is recreating the actual Java object in memory using the byte stream. This means is utilized to keep an object alive.

Before we go into serialization details, let’s look at a real-world example.

Assume you’re on the phone with a buddy via a cellular network. Your voice can be heard by a friend who is thousands of miles distant. You may also be aware that your voice is translated into electrical signals and subsequently transmitted via radio signals to a mobile tower. The tower later channels the electrical signal to the precise phone number you are connected with.

Your voice is converted many times before it reaches your companion on the other side. Similarly, when moving from one Java Virtual Machine to another, Java objects require serialization and deserialization.

No alt text provided for this image


The JVM grants the class that implements the Serializable Interface extra capabilities.

A class called “ObjectOutputStream” has a method called “writeObject.” This method is in charge of turning an item into a Byte stream. The class “ObjectInputStream” has a method called readObject (). This method turns a byte stream into a string.

Syntex

No alt text provided for this image


Java.io. Serializable Interface

Serializable is an interface that allows data to be serialized (has no data member and method). It’s used to “mark” Java classes so that their objects can have specific capabilities.

  • The class whose object has to be persisted must implement the Serializable interface.
  • The java.io package is implemented by the String class and all wrapper classes.
  • By default, the interface is serializable.

Java’s ObjectOutputStream class

For writing objects into streams, this class is required. Only classes that implement the Serializable interface can be used as arguments in this class’s methods. The object is transformed into a byte stream using the methods in this class.

Constructor in Java

Let’s have a look at constructor function of this class-

No alt text provided for this image

This function Object() creates a byte stream that writes to an Object stream.

Methods in Java

Now let us glance at the methods inside this class. This class mainly houses three methods.

  • public void flush() throws IOException {}?– This method is accountable for flushing the output stream.
  • public final void writeObject(Object obj) throws IOException {}?– responsible for writing the object to the ObjectOutputStream.
  • public void close() throws IOException {}?– This method is useful for closing the current output stream.

Java’s ObjectInputStream class

This class aims to perform the exact opposite of what the ObjectOutputStream class accomplished. Instead, it turns the byte stream into objects that can be deserialized.

Constructor in class

Let’s have look at the constructor function of this class-

No alt text provided for this image

This constructor is crucial because it produces an ObjectInputStream, which reads from the InputStream.

Methods in Java

  1. public final Object readObject() throws IOException, ClassNotFoundException{}?– This method transforms the stream to an object. This means that the method reads the object from the incoming input stream.
  2. public void close() throws IOException {}?– As the name recommends, this method has the function of closing the ObjectInputStream

What is the most reasonable way to serialize an object in Java?

Let’s move into how to serialize an object now that you know what serialization in Java is and all the important characteristics. For serialization, we have to use the ObjectOutputStream class’s writeObject() function, and for deserialization, the InputObjectStream class’s readObject() method is used.

1.?The writeObject()?method has the following syntax:

IO Exception is thrown by public final void writeObject(Object o).

2.?The readObject()?method has the following syntax:

final public presentation IOException, ClassNotFoundException is thrown by readObject().

To understand how to serialize an object in Java, consider the following example.

No alt text provided for this image
No alt text provided for this image

The Java platform offers a default method for the serialization of serializable objects. A (Java) class is a collection of instructions that describes how to do something.

What are the advantages of serialization in Java?

No alt text provided for this image

Serialization has a lot of benefits. Some of its primary usefulness is as follows:

  • This is a Java functionality that comes standard. As a result, you won’t have to rely on third-party services to implement Serialization.
  • Serialization is used in almost all of the technology we use daily.
  • This concept is simple to grasp and customize.
  • It is adaptable to the requirements of the programmer.
  • This process is universal, and all developers are familiar with the concept.
  • Java can now do encryption, authentication, compression and secure Java computing.
  • It’s used to marshal soldiers (traveling the state of an object on the network) and to save or endure the state of an object that the JVM doesn’t bind.

Some important considerations to make during the Serialization of an object in Java

There are a few prerequisites for serializing an object. These are the conditions and considerations to assume when utilizing Java serialization.

  • There are no data members or methods in the serialization interface.
  • Only the serializable interface can be used to serialize an object.
  • If the parent class executes the Serializable interface, the child class is not needed.
  • Only non-static data members are saved during the serialization process; static or temporary data members are not saved.
  • A class’s fields must all be serializable; otherwise, the temporary keyword should be used
  • String and all wrapper classes are disabled by default.

Conclusion

Hence serialization in Java means transforming a Java object into a stream of bytes. When the end-user receives this stream of bytes, it is transformed into an object, which can be easily transported from one JVM to another. It’s worth noting that the byte stream is platform-agnostic. You can change a stream of bytes into an object and run it in any context once you have it.

Upcoming Event

Free Webinar on Selenium with Python

No alt text provided for this image

Register for the ????FREE Webinar on Selenium with Python

https://www.devlabsalliance.com/webinar-on-selenium-with-python/

Topics covered in this webinar:

?? What is SDET?

?? Why there is so much hype around it?

?? Future of SDET with Python

?? Job opportunities in the global market

?? Practical demo - SDET with Python

Don't miss the opportunity to learn from industry experts and real-time practitioners with ZERO investment.

Register here and we will see you inside:

https://www.devlabsalliance.com/webinar-on-selenium-with-python/

New Batch Update: Certified SDET Professional

No alt text provided for this image


DevLabs Alliance ???? ???????????????? ?? ?????? ?????????? ?????? "???????????????????? SDET ???????????????????????? ????????????????"

????????????? ???? ?????????? ?????? ?????? ???????????? ????????????!!

(SDET Professional batch starts from 24th Jun ,23)

?Let’s join our complete ??????????????,????????????,???????????????????? ?????? ?????? ??????????????s ?????????? ???????????????????????? ?????????????????????? ???????????????? ??????????????

?Special ???????????????? ???? ?????????????????? ?????? ?????????????? ????????

????????????????????? ?????????????????????????? ?????? ????????-

https://lnkd.in/fHqgGED?utm_source=Linkedin&utm_medium=Course+Marketing&utm_campaign=Course+Promotion&utm_id=DLA+Course+Marketing


????????????????:

?Training Mode – Live Interactive Virtual Mode

?Training will conduct only on Saturday & Sunday

?Faculty would be Industry Veterans having 15+ years of overall Industry exposure

?Industry Standard Certification - Certified SDET Professional

?24*7 Technical support

New Job Update

  • Position Name: Automation Tester
  • Experience: 4 to 8yrs
  • WFH
  • Budget: 60k to 70k negotiate
  • Contract
  • Passport is mandatory.

JOB SUMMARY

Highly skilled test automation engineer to design automation tests. Test automation engineers’ duties include designing automation scripts and finding solutions for automation problems. You should also be committed to developing skills in order to provide up-to date solutions. To be successful as a test automation engineer, you should be exceptionally accurate and possess a superior mathematical aptitude. Ultimately, a top-notch test automation engineer will be proficient in programming languages and have excellent time management skills.

  • At least 4-5 years of experience as an automation tester.
  • Should have good Programming Skills and Automation mindset.
  • Knowledge of techniques for the verification and validation of the quality of products and processes across SDLC & STLC.
  • Requirements tracing and validations during Quality Control testing. Should be highly proficient at debugging issues.
  • Experience in API testing, Mobile Testing and Web Testing.
  • Should be Proficient in writing Web and API automated tests.
  • Should have excellent understanding of Automation Tools like Selenium with Java.

Role & Responsibility

Familiarity with all aspects of Quality Control testing of software, including functional, and regression testing. Collaborating with software developers, other QA, and product owners from user story inception to completion.

Interested candidate can share there resume at [email protected]



要查看或添加评论,请登录

社区洞察

其他会员也浏览了