Practical use of wrapper classes

 1. Implement the conversion between int and Integer classes

    In the actual conversion, use the constructor of the Integer class and the intValue method inside the Integer class to realize the mutual conversion between these types. The implementation code is as follows:

                   int n = 10;

                   Integer in = new Integer(100);

                   // Convert int type to Integer type

                   Integer in1 = new Integer(n);

                   // Convert object of type Integer to type int

                   int m = in.intValue();

 2. Common methods inside the Integer class

              The Integer class contains some methods related to int operations. Here are some of the more commonly used methods:

                   a. parseInt method

                            public static int parseInt(String s)

      What this method does is convert a numeric string to an int value. In future interface programming, converting a string to a corresponding int number is a relatively common operation. The usage example is as follows:

                                     String s = “123”;

                                     int n = Integer.parseInt(s);

      Then the value of the int variable n is 123. This method actually implements the conversion between strings and int. If the strings do not contain all numeric characters, the program execution will be abnormal.

        Another parseInt method:

                public static int parseInt(String s, int radix)

          Then the implementation converts the string to int according to the base specified by the parameter radix. The usage example is as follows:

                 // Convert the string "120" to int in decimal, the result is 120

                int n = Integer.parseInt(“120”,10);

                 // Convert the string "12" to int in hex, the result is 18

              int n = Integer.parseInt(“12”,16);

                 // Convert the string "ff" to int in hex, the result is 255

                int n = Integer.parseInt(“ff”,16);

          This allows for more flexible conversions.

                   b. toString method

                            public static String toString(int i)

                            The function of this method is to convert the int type to the corresponding String type.

                            Use the sample code as follows:

                                     int m = 1000;

                                     String s = Integer.toString(m);

                            Then the value of the string s is "1000".

                            Another toString method implements converting an int value to a string in a specific base:

                                     public static int parseInt(String s, int radix)

                            Use the sample code as follows:

                                     int m = 20;

                                     String s = Integer.toString(m);

                            Then the value of the string s is "14".

            In fact, since version 1.5 (5.0) of JDK, the syntax of automatic unboxing has been introduced, that is, when converting basic data types and corresponding wrapper classes,

      The system will do it automatically, which will greatly facilitate the programmer's code writing. Use the sample code as follows:

                   //int type will be automatically converted to Integer type

                   int m = 12;

                   Integer in = m;

                   //Integer type will be automatically converted to int type

                   int n = in;

         Therefore, the type conversion in actual use will become very simple, and the system will automatically implement the corresponding conversion.

 

byte[]a=new byte[100];

After the above statement is defined, the default value in the a array is 100 0s, because byte is the basic data type


Byte[] b=new Byte[100];
After the above statement is defined, the default value in the a array is 100 nulls, because Byte is an object data type


when defining an array, if it is a basic data type int, byte, boolean, long, float, double, the default value of each element is 0, the default value of char data type is space, and if it is an object data type then the default value is null

Guess you like

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