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?
Asheesh_KumarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.