Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5840951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:49:26+00:00 2026-05-22T11:49:26+00:00

I am learning java, and studying the following example from sun.com. import java.io.*; import

  • 0

I am learning java, and studying the following example from sun.com.

import java.io.*;
import java.util.*;

public class UserInfo implements Serializable {
   String name = null;

   public UserInfo(String name) {
   this.name = name;
 }

 public void printInfo() {
    System.out.println("The name is: "+name);
 }
}




import java.io.*;
import java.util.Date;

public class ReadInfo {

public static void main(String argv[]) throws Exception {
    FileInputStream fis = new FileInputStream("name.out");
    ObjectInputStream ois = new ObjectInputStream(fis);

    UserInfo user1 = (UserInfo) ois.readObject();
    UserInfo user2 = (UserInfo) ois.readObject();

    user1.printInfo();
    user2.printInfo();
    ois.close();
    fis.close();
 }
 }

I am having question regarding the ReadInfo.java. In specific, I do not know how to understand the code of line UserInfo user1 = (UserInfo) ois.readObject(); Especially, what is the functionality of “UserInfo” in the parenthesis. What is the relationship between (UserInfo) and ols.readObject( ).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T11:49:27+00:00Added an answer on May 22, 2026 at 11:49 am

    The Object is being read and the code is casting it to a UserInfo Object, as there is some knowledge by the code author that “name.out” contains serialized Objects of type UserInfo.

    The API makes this very clear:

    The method readObject is used to read
    an object from the stream. Java’s safe
    casting should be used to get the
    desired type. In Java, strings and
    arrays are objects and are treated as
    objects during serialization. When
    read they need to be cast to the
    expected type.

    Additionally you might want to read up on the Java tutorial’s section re: Casting Objects.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I was learning Java coming from a background of some 20 years of
Total noobie question here. I'm just learning Java, and studying passing arguments to functions.
I'm learning to use Java's Pattern and Matcher, and this is an example code
Im learning Java and having a problem with ArrayList. Firstly I have a class
I'm learning Java am having trouble running an example program. I have two files:
I’m just learning Java and trying to have an array of a class. When
I am learning Java for a test (tomorrow) and am wondering about a question
So I'm learning java, and I have a question. It seems that the types
I'm learning Java (and OOP) and although it might irrelevant for where I'm at
I am a .NET dev learning Java. As a result, I understand all the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.