The role and significance of java wrapper classes

  • In the parameter list of the add method is the need of passing object type, and the number is a value type, it is necessary to packaging.
  • Easy conversion between data, such as string turn int, if it is a value type is no way to turn, with the wrapper class valueof toInt and other methods to achieve Huzhuan

After the basic data types of data packaged as packaging, can use various methods of packaging, there is the most commonly used type of data conversion, wherein the conversion between the most commonly used is integer and string :

1) converted into Integer String: if defined: int i = 123;

(1) String s1 = i + ""; // first method

(2) String s2 = Integer.toString (i); // second method

(3) String s3 = String.valueOf (i); // third method

2) converted into Integer String: if defined: String s = "456";

(1) int i1 = Integer.valueOf (s); // The first method

(2) int i2 = Integer.parseInt (s); // second method

3, Integer types of methods: toString (), toString (int i), valueOf (int i), valueOf (String str)

(1) toString () method: Returns a String object worthy of the Integer, which is a non-static method, with the need to refer to the object name.

(2) toString (int i) Method: Returns a String object specifies an integer (i) indicates that this is a static method, by using the class name Integer point.

(3) valueOf (int i) Method: Returns an Integer object specified int value indicates that this is a static method, by using the class name Integer point.

(4) valueOf (String str) Method: Returns a specified Integer String object value is represented, this is a static method, by using the class name Integer point.

Published 74 original articles · won praise 2 · Views 6460

Guess you like

Origin blog.csdn.net/weixin_42067668/article/details/105069188