47

I know what a wrapper class is, they wrap primitive types (e.g. int, double, etc) to objects of their respective class.

But, why do we need Wrapper classes in the first place? Why not simply go with primitive types where we have them?

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
zengr
  • 38,346
  • 37
  • 130
  • 192

9 Answers9

76

Several possible reasons:

  • So that a null value is possible
  • To include in a Collection
  • To treat generically / polymorphically as an Object along with other Objects
froadie
  • 79,995
  • 75
  • 166
  • 235
20

Am example of when wrappers are used would be in Collections, you can have an ArrayList<Integer>, but not an ArrayList<int> same with HashMaps etc. To get type safety we use generics and generics need objects not primitives.

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
Adam
  • 43,763
  • 16
  • 104
  • 144
13

Java is an object oriented programming language. I think you could also ask - why do we have primitives and why is everything not just an object?

Java designers kept the two separate to keep things simple. You use the wrappers when you need types that fit in the object oriented world - like polymorphism, collections etc. You use the primitives when you need efficiency.

samitgaur
  • 5,601
  • 2
  • 29
  • 33
8

Wrapper classes are used instead of primitive types when an Object is expected.

In Collections for example, an ArrayList may contain instances of any subclass of Object, but because primitive types are not Objects, they could not be contained in the ArrayList.

neXus
  • 2,005
  • 3
  • 29
  • 53
4

Wrapper classes are used to convert any primitive type into an object.The primitive data types are not objects, they do not belong to any class, they are defined in the language itself. While storing in data structures which support only objects, it is required to convert the primitive type to object first, so we go for wrapper class.

2

Wrapper Class:

  1. Java uses primitive types, such as int, char, double to hold the basic data types supported by the language.

  2. Sometimes it is required to create an object representation of these primitive types.

  3. These are collection classes that deal only with such objects. One needs to wrap the primitive type in a class.

  4. To satisfy this need, java provides classes that correspond to each of the primitive types. Basically, these classes encapsulate, or wrap, the primitive types within a class.

  5. Thus, they are commonly referred to as type wrapper. Type wrapper are classes that encapsulate a primitive type within an object.

  6. The wrapper types are Byte, Short, Integer, Long, Character, Boolean, Double, Float.

Laurel
  • 5,965
  • 14
  • 31
  • 57
1

Java is an object-oriented language and as said everything in java is an object. But what about the primitives? They are sort of left out in the world of objects, that is, they cannot participate in the object activities, such as being returned from a method as an object, and being added to a Collection of objects, etc. . As a solution to this problem, Java allows you to include the primitives in the family of objects by using what are called wrapper classes.

Pazhaniyappan
  • 122
  • 1
  • 10
1

There are three reasons that you might use a Number object rather than a primitive:

  1. As an argument of a method that expects an object (often used when manipulating collections of numbers).
  2. To use constants defined by the class, such as MIN_VALUE and MAX_VALUE, that provide the upper and lower bounds of the data type.
  3. To use class methods for converting values to and from other primitive types, for converting to and from strings, and for converting between number systems (decimal, octal, hexadecimal, binary).

Source from:

The Numbers Classes

Kun
  • 580
  • 2
  • 13
  • 1
    I don't get item 2. You shouldn't be using something like `i.MAX_VALUE` anyway. Also, `MIN_VALUE` / `MAX_VALUE` constants are of *primitive type*. – aioobe Mar 08 '18 at 07:40
0

One pragmatic reason off the top of my head is that Objects can be null, primitives cannot*. If I can't ensure that a function can return an int, using the wrapper is the only way to deal with getting the int I expect. Autoboxing takes care of the rest.

ebt
  • 1,358
  • 1
  • 13
  • 18