need of Wrapper class in java

Posted on 2008-11-04
Last Modified: 2012-05-05
what is the need of Wrapper class in java?

where to user Primitive type and where to use Wrapper class?
Question by:Asheesh_Kumar
    LVL 4

    Assisted Solution

    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.

    Author Comment

    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?
    LVL 4

    Accepted Solution

    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.

    Author Comment

    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?
    LVL 6

    Expert Comment

    Your question was answered.

    Author Closing Comment

    satisfactory solution.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    sameEnds challenge 25 56
    squareUp  challenge 22 81
    classnotfoundexception 3 28
    scoresClump  challenge 31 86
    After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    Video by: Michael
    Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
    This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now