String, Integer, int type conversion summary, integerint
Today I learned the concept of encapsulation. It is very interesting to change the data type by means of automatic packing and unpacking. At the same time, mutual conversion between them is worth remembering. Of course, practice makes perfect at work, just to sort out knowledge points for your reference:
Automatic packing and automatic unpacking have been implemented since Java 7, and the conversion between int and Integer can be achieved automatically
The following is the actual operation code.
1 public class Integer04 {2 public static void main (String [] args) {3 // int ---> Integer 4 Integer i1 = Integer. valueOf (10); // return an Integer instance that represents the specified int value. 5 // create an Integer instance 6 Integer m1 = new Integer (10) through the constructor ); // encapsulation Principle 7 8 // Integer --> int 9 int i2 = i1.intValue (); // unpack 10 11 // String --> Integer12 Integer i3 = Integer. valueOf ("10"); // return an Integer instance 13 14 indicating the specified String value. // Integer --> String15 String s1 = i3.toString (); // return a specified String 16 17 // String --> int18 int i4 = Integer. parseInt ("123"); // note that it must be a pure number 19 20 // int --> String21 String s2 = 10 + ""; 22} 23}
Int packing can be achieved through Integer I = new Integer (10), or you can directly call Integer. valueOf () method implementation. You can also use this method to convert Sring to Integer. unpacking is the intValue () method called, to convert Integer to String, the toString method is called. To convert String to int, the parse () method of Integer is required, but note the following: the String must be a pure number before conversion between them. The conversion from int to String is very simple: int + String is a String, which is the basis and easy to forget.
In this case, we feel that it is helpful for our memory and understanding!