Get the tools you need to learn Java skills fast!
Video tutorials, eBooks, hands-on lab exercises, sample code.
Get started
The moose likes Beginning Java and the fly likes question on understanding “UserInfo user1 = (UserInfo) ois.readObject();” Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "question on understanding “UserInfo user1 = (UserInfo) ois.readObject();”" Watch "question on understanding “UserInfo user1 = (UserInfo) ois.readObject();”" New topic

question on understanding “UserInfo user1 = (UserInfo) ois.readObject();”

cake naiyou

Joined: Apr 29, 2011
Posts: 27
I am learning java, and studying the following example from

I am having question regarding the 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( ).
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11689

it is called a cast.

the method is defined to return an Object reference. That means it can return ANY object. We're telling the compiler that we really know it is returning a UserInfo reference, so we're sort of forcing it to consider it as such.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
I agree. Here's the link:
subject: question on understanding “UserInfo user1 = (UserInfo) ois.readObject();”