The main purpose of AutoBoxing(unBoxing) is provide a way to work with primitives as Objects, in order we can use Generics and so on.
Find below the Wrapper classes:
Creating Wrapper Objects
Most of Wrappers provide a constructor that gets as an argument a primitive value and a String object. Only the Wrapper class Character gets one argument which is a char primitive.
Let's see some examples:
All the wrapper classes mentioned so far contain a method called valueOf(), this method is a static method and can get different arguments depending on the wrapper, for instance:
Byte/Short/Integer/Long
[Wrapper] static valueOf(primitive)
[Wrapper] static valueOf(String)
[Wrapper] static valueOf(primitive, int radix) // radix defines the base like decimal, octal or hexadecimal.
Float/Double/Boolean
[Wrapper] static valueOf(primitive)
[Wrapper] static valueOf(String)
Character
[Wrapper] static valueOf(primitive)
Conversion
Also we can make a conversion using the wrapper classes by calling the methods xxxValue(), all these methods don't have arguments, for instance:
Wrapper classes
The following classes extend Number -> Byte/Double/Float/Integer/Long/Short
Slightly different from the valueOf() method, the static method parseXXX() has two "flavours" similar to valueOf(), furthermore these methods are applicable only for the 6(six) integer classes and not to mention that the return of these is a primitive type instead of a wrapper class.
[Wrapper] static valueOf(String)
[Wrapper] static valueOf(primitive, int radix) // radix defines the base like decimal, octal or hexadecimal.
Example of valueOf, xxxValue and parseXxx
Finally the last thing that you have to pay attention is, valueOf and parseXxx throw a NumberFormatException if the format of a string is not valid.
No comments:
Post a Comment