Java Road from entry to advanced of (XIX)

In the previous article we introduce a bit in Java Object, in this chapter we look at the Java wrapper class.

There are eight in Java basic types: byte, short, int, long, float, double, char, boolean, is in the form of value, so they did not object-oriented features, is no inherited from Object, so You can not use more than one, with an angle to accept Object basic types.

 Suppose now that we have a value of 1 + 1 is calculated, as follows:

 1 public class Main {
 2     public static void main(String[] args) {
 3         int i = 1 + 1;
 4         System.out.println(i); // 2
 5 
 6         Integer integer1 = new Integer(1);
 7         Integer integer2 = new Integer(1);
 8         int integer = integer1.intValue() + integer2.intValue();
 9         System.out.println(integer); // 2
10 
11 
12     }
13 }
14 
15 class Integer {
16     private int i;
17 
18     public Integer(int i) {
19         this.i = i;
20     }
21 
22     public int intValue() {
23         return i;
24     }
25 }

In the above code, if we want to calculate the value of 1 + 1, in accordance with our previously learned directly int type 1 + 1 data can be acquired, but we said above, the basic feature types do not have object oriented that we are in some cases they need to how to do it, this is the integer class above code we've written, but if we are only to calculate a 1 + 1, each need to instantiate some of the fuss was too Integer a.

So Java is very thoughtful package into eight basic types a bit, and built-in within the lang package, namely packaging, so our code is below normal operation:

 1 public class Main {
 2     public static void main(String[] args) {
 3         int i = 1 + 1;
 4         System.out.println(i); // 2
 5 
 6         Integer integer1 = new Integer(1);
 7         Integer integer2 = new Integer(1);
 8         int integer = integer1.intValue() + integer2.intValue();
 9         System.out.println(integer); // 2
10     }
11 }

 

Wrapper class is immutable class, after construction of the wrapper class object is not allowed to change the packaging in which the value of packaging is final and can not define their subclasses.

 

 In the picture above, we can see that the type of digital wrapper class inherits from Number, char and boolean type of wrapper class inherits from Object, then we take a look at an example, the following code:

1  / * 
2  * digital wrapper class inherits from type Number The
 . 3  * into which provides: intValue, a method doubleValue in,
 . 4  * number which acts to return the current packaging represented in other types of digital form
 5  * * / 
. 6  public  class the main {
 . 7      public  static  void main (String [] args) {
 . 8          / ** 
. 9           * basic type conversion into packaging, there are two methods:
 10           * 1, call the constructor
 11           * 2, calling the static method valueOf (recommended)
 12 is           * * / 
13 is          Integer = Integer1 new new Integer (. 1 );
 14          Integer = Integer2new Integer(1);
15         System.out.println(integer1 == integer2); // false
16         System.out.println(integer1.equals(integer2)); // true
17 
18         Integer integer3 = Integer.valueOf(1);
19         Integer integer4 = Integer.valueOf(1);
20         System.out.println(integer3 == integer4); // true
21         System.out.println(integer3.equals(integer4)); // true
22 
23         Integer integer5 = Integer.valueOf(128);
24         Integer integer6 = Integer.valueOf(128);
25         System.out.println(integer5 == integer6); // false
26         System.out.println(integer5.equals(integer6)); // true
27     }
28 }

In the above code, we use the example of the first method, the same results with the results obtained when the equals method before the article talking about when we use the valueOf () method, found == also become a true, because valueOf It reuses the object, and will not re-assign an address, but only -128-- integer 127. More than once will go to new a new object. In comparison, -128 - When between 127, valueOf will help us save some memory space, it is recommended to use valueOf.

Of course wrapper class also provides us with a method into basic types, as follows:

. 1  public  class the Main {
 2      public  static  void main (String [] args) {
 . 3          Integer Integer = Integer.valueOf (128 );
 . 4  
. 5          int INT1 = Integer.intValue ();
 . 6          System.out.println (INT1); / / 128 
. 7  
. 8          a float floatl = integer.floatValue ();
 . 9          System.out.println (floatl); // 128.0
 10  
. 11          // byte type -128--127 beyond recalculates 
12 is          byte Byte1 = integer.byteValue ();
 13         System.out.println(byte1); // -128
14     }
15 }

Then how do we know that each basic type of maximum and minimum values ​​of it? Packaging is already very close to us to provide a method, as follows:

1  / * 
2  * digital type packaging support two constants
 . 3  * MAX_VALUE, MIN_VALUE
 . 4  *, respectively stored corresponding to the maximum value and the minimum value of the basic types
 . 5  * * / 
. 6  public  class the Main {
 . 7      public  static  void main (String [] args) {
 . 8          int intMax is = Integer.MAX_VALUE;
 . 9          int intMin = of Integer.MIN_VALUE;
 10          System.out.println (intMax is); // 2147483647 
. 11          System.out.println (intMin); // -2147483648 
12 is  
13 is          byte byteMax = Byte.MAX_VALUE;
14         byte byteMin = Byte.MIN_VALUE;
15         System.out.println(byteMax); // 127
16         System.out.println(byteMin); // -128
17     }
18 }

Through the above code that we understand some of the basic characteristics and use of packaging, then we use what method commonly used when packaging it? as follows:

1  / * 
2  * wrapper class provides a static method parseXXX (String STR)
 . 3  * given string can be converted to the corresponding base type
 4  * provided that the string must be accurately describe the basic types can be stored value
 . 5   * / 
. 6  public  class the main {
 . 7      public  static  void main (String [] args) {
 . 8          String STR = "123" ;
 . 9          int I = the Integer.parseInt (STR);
 10          System.out.println (I); / / 123 
. 11  
12 is          Double D = Double.parseDouble (STR);
 13 is          System.out.println (D); // 123.0
14  
15          a float F = Float.parseFloat as (STR);
 16          System.out.println (F); // 123.0 
. 17  
18 is          String str1 = "123.123" ;
 . 19          int I1 = the Integer.parseInt (str1);
 20 is          the System.out. the println (I1); // compile error, Integer does not recognize decimal 
21 is  
22 is          Double D1 = Double.parseDouble (str1);
 23 is          System.out.println (D1); // 123.123 
24      }
 25 }

In the above code, we define the "123" and "123.123" two strings can be seen from the above output, only the correct amount of string can describe the basic types of values ​​can be saved in order to run successfully.

Next we look at a wrapper class automatic entry box

1  / * 
2  * new features introduced after JDK1.5
 3  * automatic entry box
 . 4   * / 
. 5  public  class the Main {
 . 6      public  static  void main (String [] args) {
 . 7          / ** 
. 8           * automatic entry box recognized by the compiler, the virtual machine not authorized
 9           * compiler automatically replenish the source code compile time to complete the conversion of the basic type of packaging
 10           * * / 
. 11          int I = new new Integer (. 1); // compiler default .intValue added later () 
12 is          Integer II = 123; // compiler will become the default Integer.valueOf (123) 
13 is      }
 14 }

In the above code, we can see a type int can be accepted is an example of the packaging, packaging may also directly receive a primitive type value, which is automatically removable tank, to avoid the preparation process of our conversion, reducing the amount of code.

Guess you like

Origin www.cnblogs.com/weijiutao/p/12017226.html