I. conversion between basic types
1 public class TransferTest { 2 public static void main(String[] args) { 3 4 //String Turn into int: Two methods 5 String str = "100"; 6 int a1 = Integer.valueOf(str); 7 int a2 = Integer.parseInt(str); 8 9 //int Turn into String: Three methods 10 int a = 100; 11 String str1 = String.valueOf(a); 12 String str2 = Integer.toString(a); 13 String str3 = "" + a; 14 15 //long/Long Turn into String 16 long l1 = 1001L; 17 Long l2 = 100L; 18 String s1 = String.valueOf(l1);//String.valueOf(l2) 19 String s2 = Long.toString(l1);//Long.toString(l2); 20 21 //long Turn into Long 22 l2 = l1;//Automatic packing and unpacking 23 System.out.println(l2); 24 l2 = Long.valueOf(l1); 25 26 //Long Turn into long 27 l1 = l2.longValue(); 28 29 //String Turn into long 30 String number = "1234567890"; 31 //Method 1: call Long Type parseLong method 32 long num1 = Long.parseLong(number); 33 //Method 2: forced conversion 34 long num2 = new Long(number); 35 //Method 3: convert first Long Type, reuse longValue Method to long 36 long num3 = Long.valueOf(number).longValue(); 37 38 /** 39 * Long.ValueOf("String")With long The difference between parselong ("string"): 40 * Long.ValueOf("String")Return Long packing type 41 * Long.parseLong("String")Return long basic data type 42 */ 43 44 //long Turn into int 45 Long n1 = 1000 * 100 * 85900L; 46 int m1 = n1.intValue(); 47 48 //perhaps 49 long n2 =1000 * 100 * 85900L; 50 int m2 = (int)n2; 51 int m3 = Math.toIntExact(n2);//long towards int Strong rotation may cause data overflow, Math.toIntExact()Method is safer and will be thrown in case of overflow ArithmeticException 52 53 //int and Integer exchange 54 //convert i(int) to j(Integer) 55 int i = 0; 56 Integer j = Integer.valueOf(i); 57 58 //convert t(Integer) to n (int) 59 Integer t = 10; 60 int n = t.intValue(); 61 62 //char Turn into int 63 char ch = '8'; 64 int b1 = ch - '0'; 65 int b2 = ch - 48; 66 //Use packaging class: 67 Character ch2 = new Character(ch); 68 int b3 = Integer.parseInt(ch2.toString()); 69 int b4 = Integer.parseInt(Character.toString(ch2)); 70 71 //int Turn into char 72 char ch1 = (char) (a + 48); 73 74 //String Turn into char array 75 String s = "abcd"; 76 char[] chs = s.toCharArray(); 77 //String Take a single letter 78 s.charAt(0); 79 80 //char Turn into String 81 String str10 = String.valueOf(ch); 82 String str20 = Character.toString(ch); 83 84 //char Convert array to String 85 char[] chars = {'a', 'b', 'c', 'd'}; 86 String str11 = String.valueOf(chars); 87 String str21 = String.valueOf(chars[0]); 88 89 90 } 91 }
II. Integer ParseInt (s) and integer The difference between valueof (s)
1,Integer.parseInt(s) usage
1 String s1 = "1000"; 2 String s2 = "1000"; 3 int n1 = Integer.parseInt(s1); 4 int n2 = Integer.parseInt(s2); 5 if (n1 == n2) { 6 System.out.println(true); 7 }
Output:
Integer.parseInt(s) is used to parse the string s into a signed int basic type.
2,Integer.valueOf(s) usage
1 String s = "123"; 2 Integer integer1 = Integer.valueOf(s); 3 4 int i = 345; 5 Integer integer2 = Integer.valueOf(i); 6 System.out.println("integer1 : " + integer1); 7 System.out.println("integer2 : " + integer2);
Output:
Integer.valueOf(s) parses the string s (or int basic type) into integer object type, and the returned integer can call the method in the object.
3,Integer.parseInt(s) and integer The difference between valueof (s)
1 //Integer.parseInt 2 String s3 = "10000"; 3 if (Integer.parseInt(s3) == Integer.parseInt(s3)) { 4 System.out.println(true); 5 } 6 7 //Integer.valueOf: When the integer value corresponding to the string is -128~127 between 8 String s4 = "100"; 9 Integer i1 = Integer.valueOf(s4); 10 Integer i2 = Integer.valueOf(s4); 11 if (i1 == i2) { //Two objects are equal 12 System.out.println("i1 == i2"); 13 } 14 if (i1.equals(i2)) { //Of two objects value Equal value 15 System.out.println("i1.equals(i2)"); 16 } 17 18 19 //Integer.valueOf: When the integer value corresponding to the string is not-128~127 between 20 String s5 = "1000"; 21 Integer i3 = Integer.valueOf(s5); 22 Integer i4 = Integer.valueOf(s5); 23 if (i3 != i4) { //Two objects are not equal 24 System.out.println("i3 != i4"); 25 } 26 if (i1.equals(i2)) { //Of two objects value Equal value 27 System.out.println("i3.equals(i4)"); 28 }
Output:
Integer.parseInt(s) parses the same string and gets the same basic type data of int. you can directly judge whether it is equal by "= =".
Integer.valueOf(s) when parsing the same string many times, the object obtained is an integer type object. Sometimes the object obtained is the same object or different objects. It needs to be determined according to the integer value of the string to be parsed: if the integer value corresponding to the string is between - 128 ~ 127, the object of integer type parsed is the same object; If the integer value corresponding to the string is not between - 128 ~ 127, the parsed object of integer type is not the same object. Whether the values in the object are equal or not.
In addition, int is a basic type and does not contain the equals method, so it can only be compared with "= =". The basic type uses "= =" to compare the size of two values.
Why integer What happens to valueof (s)?
This is because the source code in JDK has been defined. Because integer values between - 128 and 127 are frequently used, when an integer object with a value between - 128 and 127 is created each time, the object is directly obtained from the cache. Therefore, integer objects with the same value are the same object in the cache- Integer values other than 128 ~ 127 are not used too frequently. Every time an integer object with the same value is created, an object is re created, so the created object is not the same object. This can be seen from the source code of JDK:
1 /** 2 * External integer Valueof method 3 */ 4 public static Integer valueOf(String s) throws NumberFormatException { 5 return Integer.valueOf(parseInt(s, 10)); 6 } 7 8 public static Integer valueOf(int i) { 9 //IntegerCache.low by -128; IntegerCache.high The default is 127, but you can JVM For configuration, the default value is 127 10 if (i >= IntegerCache.low && i <= IntegerCache.high) 11 //If i stay-128~127 Fetching objects from the cache 12 return IntegerCache.cache[i + (-IntegerCache.low)]; 13 //If i be not in-128~127 Between, recreate an object 14 return new Integer(i); 15 }
flow chart:
Conclusion:
1,Integer.valueOf returns the wrapped integer object, and there is a limit of - 128 ~ 127 to cache, integer ParseInt simply returns an int value
2,Integer.parseInt only accepts parameters of String type, integer Valueof can receive int and String (wrap int as integer)
reference resources: https://blog.csdn.net/u010502101/article/details/79162587