wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Usage of Wrapper Classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Usage of Wrapper Classes" Watch "Usage of Wrapper Classes" New topic
Author

Usage of Wrapper Classes

Karthikeyan Ramaswamy
Ranch Hand

Joined: Apr 30, 2008
Posts: 41
hi all,


what is the main advantage of using wrapper classes?

and when should we use wrapper class?



thank you
Ulrika Tingle
Ranch Hand

Joined: Nov 24, 2009
Posts: 92
Karthikeyan Ramaswamy wrote:
what is the main advantage of using wrapper classes?

and when should we use wrapper class?


Do you mean the wrapper classes corresponding to the Java primitives?

They should be used in situations when primitives cannot be used. The main situation is when you want to store primitives in one of the Java collections (which cannot handle primitives).

The use of the wrapper classes is greatly simplified by something called autoboxing. This means that Java automatically determines whether a primitive or a corresponding wrapper should be used. So it's rare that you need to use the wrapper classes explicitly.

Say you do,

ArrayList<Integer> al = new ArrayList<Integer>();

Integer is the wrapper for int. Thanks to autoboxing you can now store ints in the above Arraylist. Or rather Java will perform all necessary conversions back and forth between Integer and int.

Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

wrapper classes are nothing but creating object for primitive values


primitive class

int
char
float
boolean


Wrapper class coreesonding to their respective primitives

Integer
Charecter
Float
Boolean

wrapping takes place when you want to use primitive values as object


http://www.lifesbizzare.blogspot.com || OCJP:81%
swapnl patil
Ranch Hand

Joined: Aug 13, 2007
Posts: 80
Wrapper classes were introduced with the discussion of the primitive data types. Primitive values in Java are not
objects. In order to manipulate these values as objects, the java.lang package provides a wrapper class for each of the primitive data
types. All wrapper classes are final. The objects of all wrapper classes that can be instantiated are immutable, that is, their state cannot be
changed.
Although the Void class is considered a wrapper class, it does not wrap any primitive value and is not instantiable (i.e., has npou blic
constructors). It just denotes the Class object representing the keyword void. The Void class will not be discussed further in this section.
In addition to the methods defined for constructing and manipulating objects of primitive values, the wrapper classes also define useful
constants, fields, and conversion methods.
Shobhit Khandelwal
Greenhorn

Joined: Feb 19, 2009
Posts: 24
1) It is sometimes easier to deal with primitives as objects.
2) Moreover most of the collection classes store objects and not primitive data types.
3) The wrapper classes provide many utility methods also.
Because of these resons we need wrapper classes. And since we create instances of these classes we can store them in any of the collection classes and pass them around as a collection. Also we can pass them around as method parameters where a method expects an object.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Usage of Wrapper Classes