• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wrapper Class?

 
shaf maff
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys

Whats a wrapper class ?
 
Wayne L Johnson
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java has eight primitive classes (boolean, byte, short, int, long, char, float, double) which traditionally have been treated differently than all other object instances. For example, if you have a collection (List, Map, etc) you can store any type of object instance, but you can't store primitives. [NOTE: I'll get to auto-boxing in a minute].

The solution is to use wrapper classes, which are in the "java.lang" package and, in general, have the same name as the primitive. Thus you have classes named Boolean, Byte, Short, Integer, Long, Character, Float and Double. These classes "wrap" -- i.e., contain -- a primitive. Traditionally if you wanted to store an "int" value into an ArrayList, you have to do something like (assume a list named "MyList"):


With newer versions of Java (JDK 1.5/5.0 and later), a feature called "autoboxing" has been added, which does this automatically for you. In other words, you can add a primitive to a collection directly, and the JVM will automatically stick it into the appropriate wrapper class for you.

Look in the JDK 1.5 documentation, in the "new feature" section, and you can read all about it.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64721
86
IntelliJ IDE Java jQuery Mac Mac OS X
 
shaf maff
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys, so a wrapper class is basically a way to store primitives in a list or something, yes?

The reason I asked was becauase I am trying to understand the following line:

"Create a mappable class that will be used as a ``wrapper'' class for an object that can be mapped. This will be used to wrap the Char objects".

I dont really understand what is meant by this, can someone break it down for me please ?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64721
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shaf maff wrote:Thanks guys, so a wrapper class is basically a way to store primitives in a list or something, yes?

That is just one of the many many many applications of the wrapper/adapter pattern.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic