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

Is it really needed to do Java training before graduation, to get a job?
Yes, It's must to get better opportunities and to standard alone in IT field. Not only core Java but also learn Spring,Hinernat ,Struts. ANGULARJS add an advantage to get job easily.
Udaybhan
How do people become Java programmers?
Before you become a Java programmer, it's important that you earn an education and learn how to code using Java. While some programmers learn how to use Java on their own, you can learn it much faster...
Rishav
0 0
5
best source to learn java from?
Main source if information on Java is oracle documentation. But it takes your time to read through, a lot more than you expect. Better prefer a trainer who can give what you need.
Himanshu
Please suggest some books to self-learn Java at home?
For self learning the best book is Core Java written by Gary Cornell and Kay Horstmann. This book comes in 2 volumes and both are important to learn the subject from the ground up to the advanced level.
Sharmistha
1 0
6

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

Ask a Question

Related Lessons

Importance of Constructor's Visibility.
While developing program or implementing Singleton pattern we have learnt to mention constructor as private and known reason is -> to blocked its direct call from outside of class. But did we think...
M

Introduction to Course Content
Video about what we are going to learn throughout the Java Training Session .

Java Advantages
In this video, learn about Java and its advantages. Also, check out the difference between Java and C++, Java development kit, Java Run Time Environment (JRE) with a proper demonstration program for better clarity.

Introduction to Programming Languages
What is a Programming Language? A programming language is a formal computer language or constructed language designed to communicate instructions to a machine, particularly a computer. Programming languages...

Difference Between Checked And Un-Checked Exception
1. Exception is an abnormal condition in the program, if raises program terminates abnormally. 2. Exception always occurs at runtime only, there is no chance of exception at compile time. 3. Syntactical...

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