The Number
subclasses wrap primitive numeric types (Byte
, Integer
, Double
, Float
, Long
, and Short
).
What purpose do they serve?
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.
Each primitive type has a corresponding wrapper: byte, short, int, long, float, double, boolean, char.
Wrapper classes are fundamental in Java because they help a Java program be completely object-oriented. The primitive data types in java are not objects, by default. They need to be converted into objects using wrapper classes.
Those wrapper classes were created so that there was some way to use those primitive types with various container classes like ArrayList
. Since primitive types can't directly be coerced into Object
references, they are stored in wrapper classes to allow them to be used where Object
references are required.
Because the wrappers are Objects
.
Collections
needs Objectsinstantiated
to nullNullPointerException
instead of strange behavior if you for example instantiate to -1 in a primitiveIf 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