wood burning stoves 2.0*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Wrapper Class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Wrapper Class?" Watch "Wrapper Class?" New topic
Author

Wrapper Class?

shaf maff
Ranch Hand

Joined: Sep 07, 2008
Posts: 180
Hi Guys

Whats a wrapper class ?
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
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

Joined: Jan 10, 2002
Posts: 61106
    
  66

More generally...


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
shaf maff
Ranch Hand

Joined: Sep 07, 2008
Posts: 180
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

Joined: Jan 10, 2002
Posts: 61106
    
  66

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Wrapper Class?