Two Laptop Bag*
The moose likes Java in General and the fly likes Wrapper classes! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Wrapper classes!" Watch "Wrapper classes!" New topic
Author

Wrapper classes!

ram kumar
Ranch Hand

Joined: May 22, 2008
Posts: 146
Could some one help me know

! What is wrapper class ?
! When do we use wrapper classes?
! Difference between wrapper classes and Normal classes?

Hope ! Sun documentation has not given information on this!

I know that to convert a primitive type in to java object and the reverse we use wrapper classes.

(Is that the only place, where i can use wrapper classes ! )

looking forth to discussion!



Discussion - the powerfull way to excellence!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38412
    
  23
More of a beginner's question. "Wrapper class" has two meanings.
  • A class with a similar name to a primitive type, which encapsulates the value of that primitive, and provides methods which relate to that sort of primitive.
  • A class which calls methods of something else, and adds an additional layer of functionality.
  • The first sort includes Integer, Double, etc, and there are (I think) also classes like Void. Also an interface called NullType.

    In the 2nd sort you could have a List and surround it with a class which calls all the List methods, but makes sure that all the calls become thread-safe. Look in the Collections class for methods like synchronizedList.

    Hope that helps
    ram kumar
    Ranch Hand

    Joined: May 22, 2008
    Posts: 146
    Originally posted by Campbell Ritchie:
    More of a beginner's question. "Wrapper class" has two meanings.
  • A class with a similar name to a primitive type, which encapsulates the value of that primitive, and provides methods which relate to that sort of primitive.
  • A class which calls methods of something else, and adds an additional layer of functionality.
  • The first sort includes Integer, Double, etc, and there are (I think) also classes like Void. Also an interface called NullType.

    In the 2nd sort you could have a List and surround it with a class which calls all the List methods, but makes sure that all the calls become thread-safe. Look in the Collections class for methods like synchronizedList.

    Hope that helps

    That was good campbell.

    But what does it mean when someone says use wrapper classes for DataBase connections and various other functions relating to databases like,
    querying / closing the connection / etc . . .

    Can you give me an example, that would say implement the wrapper classes in data base realted activities.
    vinnu kumar
    Greenhorn

    Joined: Sep 10, 2007
    Posts: 12
    When ever we need to send some data on internet,
    we use WrapperClasses.
    Data that is transferred should be int form of an object..
    int a;if we want this 'a' to send over net it should be converted into object..
    Integer a=(Integer)a;
    Charles Lyons
    Author
    Ranch Hand

    Joined: Mar 27, 2003
    Posts: 836
    Just to clarify so we don't confuse anyone:
    When ever we need to send some data on internet, we use WrapperClasses. Data that is transferred should be in form of an object..
    That's actually the reverse of what would be done. All data sent over 'the wire' is done in a binary protocol (how else would it be sent after all?) - so all primitive types are all fine as they are. What aren't fine just by themselves are objects - so these need to be sent via serialisation or RMI, which convert objects into binary stream data which can be unpacked at the receiving end. The serialisation of the Integer wrapper class is, you've guessed it, just an int - the most compact way to send the primitive.

    I think what you might be referring to is in the Java EE Web container where we add objects as scoped attributes or as headers in the HTTP classes - i.e. storing primitives as objects in Collections like Maps. In that case we have to wrap primitives up so they can be stored as Objects. This is done implicitly in Java SE 5 and above by auto-(un)boxing so no longer becomes such a huge issue as it once was.


    Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
    Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
     
     
    subject: Wrapper classes!