• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am learning java, and studying the following example from sun.com.





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( ).
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic