Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are there wrapper classes in Java? [duplicate]

Tags:

java

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?

like image 830
zengr Avatar asked Aug 26 '10 20:08

zengr


People also ask

Why do wrapper classes exist in Java?

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.

What is the purpose of wrapper classes like Integer and double in Java?

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.

What is the point of wrapper classes?

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.

What is wrapper class double?

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.


1 Answers

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
like image 119
froadie Avatar answered Sep 17 '22 21:09

froadie