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?
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?
Several possible reasons:
Am example of when wrappers are used would be in Collections, you can have an ArrayList<Integer>
, but not an ArrayList<int>
same with HashMap
s etc. To get type safety we use generics and generics need objects not primitives.
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.
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.
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.
Java uses primitive types, such as int, char, double to hold the basic data types supported by the language.
Sometimes it is required to create an object representation of these primitive types.
These are collection classes that deal only with such objects. One needs to wrap the primitive type in a class.
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.
Thus, they are commonly referred to as type wrapper. Type wrapper are classes that encapsulate a primitive type within an object.
The wrapper types are Byte, Short, Integer, Long, Character, Boolean, Double, Float.
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.
There are three reasons that you might use a Number object rather than a primitive:
Source from:
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.