Common methods of wrapper classes

Overview of Wrapper Classes: Eight Types

byte-Byte;      boolean-Boolean;    short- Short;        char-Character; 

int-Integer;     long-Long;               float-Float;           double-Double  

There are two main purposes of wrapper classes:

>The wrapper class exists as a class corresponding to the basic data type, which is convenient for the operation of the object.

> The wrapper class contains the relevant properties of each basic data type, such as the maximum value, the minimum value, etc., and the related operation methods.

The usage of the wrapper class, taking Integer as an example:

Construction method:

>  public Integer(int value)
//Constructs a newly allocated  Integer  object that represents the specified  int  value.
Parameters:
value  - the value to be represented by the  Integer  object.
> public Integer(String s)
        throws NumberFormatException
Constructs a newly allocated  Integer  object that represents the  int  value indicated by the  String  parameter. The string is converted to an  int  value in exactly the manner used by the  parseInt  method for radix 10.
Parameters:
s  - the  String  to be converted to an  Integer .
Throws:
NumberFormatException  - if the  String  does not contain a parsable integer.
See Also:
parseInt(java.lang.String, int)

Basic data types can be converted to wrapper classes through constructors.

Common method:

1. Use the .parseInt(String s) method in the Integer class to convert the string to int type:

public static int parseInt(String s)
                    throws NumberFormatException
Such as:

int num = Integer.parseInt("12345");

2. Use the .parseInt(String s, int radix) method in the Integer class to parse s with the base (binary, decimal, hexadecimal, etc.) provided by the second parameter and return

Data of type int:

public static int parseInt(String s,
                           int radix)
                    throws NumberFormatException

Such as:

parseInt("0", 10) returns 0
 parseInt("473", 10) returns 473
 parseInt("+42", 10) returns 42
 parseInt("-0", 10) returns 0
 parseInt("-FF", 16) returns -255
 parseInt("1100110", 2) returns 102
 parseInt("2147483647", 10) returns 2147483647
 parseInt("-2147483648", 10) returns -2147483648
 parseInt("2147483648", 10) throws a NumberFormatException
 parseInt("99", 8) throws a NumberFormatException
 parseInt("Kona", 10) throws a NumberFormatException
 parseInt("Kona", 27) returns 411787
3. Use the .valueOf(String s) method in the Integer class to convert the string to an Integer type

public static Integer valueOf(String s)
                       throws NumberFormatException
Such as:

Integer in = Integer.valueOf("3455");

4. Use the .valueOf(int i) method in the Integer class to convert the int type to the Integer type

public static Integer valueOf(int i)

For example: Integer in = Integer.valueOf(12);//Only for understanding, the compiler will automatically convert

5. Use the .valueOf(String s, int radix) method in the Integer class to parse s with the base (binary, decimal, hexadecimal, etc.) provided by the second parameter 

Returns an Integer object

public static Integer valueOf(String s,
                              int radix)
                       throws NumberFormatException
Such as: Integer a = Integer.("1234",10);//The return result is 1234

       Integer b = Integer.("00001010",2);//The return result is 10





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325200372&siteId=291194637