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?
Wrapper Class will convert primitive data types into objects. The objects are necessary if we wish to modify the arguments passed into the method (because primitive types are passed by value). The classes in java. util package handles only objects and hence wrapper classes help in this case also.
The Integer class and Double class are wrapper classes that create objects from primitive types. The following Integer methods and constructors, including what they do and when they are used, are part of the Java Quick Reference. Integer(value): Constructs a new Integer object that represents the specified int value.
Wrapper classes are used to convert any data 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. Sometimes, it is required to convert data types into objects in Java language.
Double class is a wrapper class for the primitive type double which contains several methods to effectively deal with a double value like converting it to a string representation, and vice-versa. An object of Double class can hold a single double value.
Several possible reasons:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With