UrbanPro

Learn Java Training from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Explain the purpose of the ObjectInputStream and ObjectOutputStream classes.

Asked by Last Modified  

2 Answers

Learn Java

Follow 2
Answer

Please enter your answer

The ObjectInputStream and ObjectOutputStream classes in Java are part of the Java Input/Output (I/O) system and are used for reading and writing objects, respectively. These classes are primarily designed for object serialization, which is the process of converting an object's state into a byte stream...
read more

The ObjectInputStream and ObjectOutputStream classes in Java are part of the Java Input/Output (I/O) system and are used for reading and writing objects, respectively. These classes are primarily designed for object serialization, which is the process of converting an object's state into a byte stream and reconstructing the object from that byte stream. Serialization is essential for scenarios like storing objects in files, sending objects over a network, or saving object states between different program executions.

  1. ObjectOutputStream:

    • Purpose: This class is used to serialize objects. It takes an object and writes its state to a stream in a platform-independent and standardized format.
    • Methods: The key method is writeObject(Object obj), which writes the specified object to the underlying stream.
    import java.io.Serializable;

    public class MyClass implements Serializable {
    // Class members and methods
    }

  • ObjectInputStream:

    • Purpose: This class is used to deserialize objects. It reads a byte stream and reconstructs the original object, restoring its state.
    • Methods: The primary method is readObject(), which reads the next object from the input stream and returns it as a generic Object. The caller must cast the returned object to its appropriate type.
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("data.ser"));
    MyClass obj = (MyClass) ois.readObject();
    ois.close();

It's important to note that for an object to be serializable, its class must implement the Serializable interface. This interface acts as a marker, indicating that the class can be serialized. If a class does not implement Serializable, an exception (NotSerializableException) will be thrown at runtime.

Here's a simple example of a serializable class:

java
import java.io.Serializable;

public class MyClass implements Serializable {
// Class members and methods
}

These classes are powerful tools for handling object persistence and communication in Java, allowing objects to be easily saved, transmitted, and reconstructed.

 
 
 
read less
Comments

Wroking in IT industry from last 15 years and and trained more than 5000+ Students. Conact ME

ObjectInputStream reads objects, ObjectOutputStream writes objects; facilitate object serialization/deserialization in Java.
Comments

Related Questions

Why does â??5/2 = 2â?? and not 2.5 in Python?
Due to dynamic Typing. use Numpy , and declare data type , you will get desired result
Srikanth
0 0
6
What are the advantages of learning Java?
Dear Niteen Java is one of the most widest implemented technology for programming. Not only its used to develop full fledged Software Applications, but its also used for programming devices like Set...
Niteen
0 0
6
Can finally block be used without try block?
No. But it can be used without a catch block.
Sarala
Is JAVA training only for technical students or a non-technical student also may join?
Any Body can Learn Programming Language.It All depends upon the learning of basics and all.Basics actually makes the thing different.
Vikrant
0 0
6
What should I do after learning Java core?
After learning Java SE you should focus on building something to make your concepts clear. You can also learn Java related frameworks like Spring to build web applications.
Kumarnaik
0 0
5

Now ask question in any of the 1000+ Categories, and get Answers from Tutors and Trainers on UrbanPro.com

Ask a Question

Related Lessons

Class and Objects in Java
Class is a template or a blueprint which is used to describe an object. On other hand Object is a reference of a class which follows all the stuff written inside the class. How about taking the whole tour in the following video

doWhile example in Java
public class doWhilePracticleEx { public void test() { } public static void main(String args) { String q1 = "Who is PM of India?"; String a1 = "Sonia Gandhi"; String a2 = "Rahul Gandhi"; String a3...
S

Sarthak C.

0 0
0

Constructor in Java
A constructor is a particular type of method that is used to initialise the object. The constructor is invoked at the time of object creation. It constructs the values, i.e. provides data for the object...

CoreJAVA
Core Java Training High Level Course Content Trained by Java Architect 1. Core Java Programming Introduction of Java 2. Data types and Operators 3. Control Flow statements 4. OOPS and its application...
A

Android : Application Launch time improvements.
For any standard android application, below 3 components play important roles to show 1st interface to user, so that he/she can interact with the app. 1. Custom Application class : Intialize the components...

Recommended Articles

In the domain of Information Technology, there is always a lot to learn and implement. However, some technologies have a relatively higher demand than the rest of the others. So here are some popular IT courses for the present and upcoming future: Cloud Computing Cloud Computing is a computing technique which is used...

Read full article >

Designed in a flexible and user-friendly demeanor, Java is the most commonly used programming language for the creation of web applications and platform. It allows developers to “write once, run anywhere” (WORA). It is general-purpose, a high-level programming language developed by Sun Microsystem. Initially known as an...

Read full article >

Java is the most commonly used popular programming language for the creation of web applications and platform today. Integrated Cloud Applications and Platform Services Oracle says, “Java developers worldwide has over 9 million and runs approximately 3 billion mobile phones”.  Right from its first implication as java 1.0...

Read full article >

Java is the most famous programming language till date. 20 years is a big time for any programming language to survive and gain strength. Java has been proved to be one of the most reliable programming languages for networked computers. source:techcentral.com Java was developed to pertain over the Internet. Over...

Read full article >

Looking for Java Training Classes?

Learn from the Best Tutors on UrbanPro

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you
X

Looking for Java Training Classes?

The best tutors for Java Training Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Java Training with the Best Tutors

The best Tutors for Java Training Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more