• 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

what is wrapper class?

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Wrapper classes are basically used to wrap the primitives into objects and then various
methods can be invoked over them. Additionally, wrapper classes play an important role in concepts
like Auto-boxing. Wrapper classes prior to java 5 were used to wrap the primitives into objects and
then use those primitives as objects in side the collection.

Hope this helps,





 
rohit shekhar
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ya but please give some complete example to understand it in a clear way...
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For example,

Wrappers major use is to convert the primitives into objects. For example



Here in snippet one int primitive has a corresponding wrapper class as Integer. Every primitive has a wrapper class.
For a detailed discussion, have a look at it this and this

Additionally what book you are following for preparing SCJP? K&B chapter 3 is an excellent reference about wrapper classes.

Hope this helps,
 
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wrapper classes are like the following:

boolean -> Boolean
int -> Integer
byte -> Byte


Left side being primitive, right side being associated wrapper class.
 
rohit shekhar
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ya following the same book but ... 'm trying to understand... thanks
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

rohit shekhar wrote:ya following the same book but ... 'm trying to understand... thanks



You are welcome mate.

Happy Preparation,
 
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wrapper classes are object type representation of primitive data types. Wrapper classes are introduced in java to provide a capability of performing various types of funtions on primitived data type by treating them as Objects, e.g : using the primitives for collections etc.
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very generally a wrapper class is a class that contains "something" in order to make it easy to use that "something". The "something" can be another class, a primitive type or a functionality from some other programming language. The wrapper class provides its own set of methods (its own API) to use that "something".

Wrapper classes for over twenty years have been used in a lot of object-oriented programming languages and so there is a design pattern called the "Adapter Pattern", which serves as a standard guide for creating wrapper classes.
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Larry Chung wrote:
is a design pattern called the "Adapter Pattern", which serves as a standard guide for creating wrapper classes.



Larry, great insight mate. I will definitely look at Adapter pattern. Didn't know this information before.

Cheers,
 
Larry Chung
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Prithvi, I started seeing wrapper classes when programming in C++ and then with Microsoft Foundation Classes. There were companies that made lots of money by selling wrapper classes for programmers to use. Things are so much better with Java and the free wrapper classes it provides.
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats why Java rocks i believe.

Best Regards,
 
Larry Chung
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prithvi Sehgal wrote:Thats why Java rocks i believe.

Best Regards,


Long live Java and all its wrapper classes!
reply
    Bookmark Topic Watch Topic
  • New Topic