• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1207
  • Last Modified:

need of Wrapper class in java

what is the need of Wrapper class in java?

where to user Primitive type and where to use Wrapper class?
0
Asheesh_Kumar
Asked:
Asheesh_Kumar
  • 3
  • 2
2 Solutions
 
WoodzCommented:
A wrapper class is used to encapsulate a primitive data type object. A primitive is a variable which is passed by value instead of by address, and is the majority of built-in data types (e.g. int, bool, long, float, char, etc..). Data types which have methods on (e.g. String), are not a primitive - they extend from object.

However, if you want to use an int (for example) in a function or data structure that is general, having a data type of object, then this does not work, because primitives do not extend from object.

What you need to do is use or create a wrapper class, which is simply a class with a single variable of the desired primitive type. This is exposed via methods, and because all custom classes extend from object, this can be used in scenarios where a general object is expected.

Hope this helps.
0
 
Asheesh_KumarAuthor Commented:
I want to know why the concept of Wrapper class is introducted in JAVA?
where we need to use Promitive type and where we need to use Wrapper class?
0
 
WoodzCommented:
The concept of a wrapper class is introduced in Java to allow general data structures and methods to be used with primatives. You cannot have a List of ints, because List requires an Object class or subclass, which int (a primitive) is not. A wrapper is a class (automatically extending Object) which wraps the primitive, allowing Integers to be added to a List.

You would use primitives for the simple data types in your classes, and when required to pass them to a method that expects an Object, wrap them in your wrapper class, and pass the wrapper class to the method.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Asheesh_KumarAuthor Commented:
I want to know why the concept of Wrapper class is introducted in JAVA?
where we need to use Promitive type and where we need to use Wrapper class?
0
 
javaexpertoCommented:
Your question was answered.
0
 
Asheesh_KumarAuthor Commented:
satisfactory solution.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now