Possible Duplicate:
Wrapper class in java
I learnt about wrapper class that it is used to wrap the primitive values. Can any of you explain me about Wrapper class in detail? Why should we go for wrapper class and when?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You need a wrapper class (like Integer, or Byte) when you want to use a primitive data type in a place where only Objects are accepted (for example to add them to a Collection).
There is no class Wrapper, but you have a separate class for every primitive type (Integer for int, Boolean for bool, Byte for byte, and so on).
How these wrapper classes work is that they are objects that have a instance field with the primitive they wrap. Also, in Java5 the compiler knows how to automatically convert between them and the primitives (which is called boxing/unboxing).
When designing an API, you should take a primitive as a method argument when it is not an optional parameter. When it is optional, you should use a wrapper so that null can be used.
For example