Similarities and Differences Integer.parseInt (s), Integer.valueOf (s) with new Integer () of

We in the development process, often need to convert the String data type to Integer, and more common way is --nteger.parseInt (s), Integer.valueOf (s) with the new Integer (), then they have those specific similarities and differences it?

First of all let me introduce three ways to use it.

Integer.parseInt (s) usage

The Integer.parseInt effect (s) of the string s is parsed into a signed basic type int.

String s1 = "1234";
String s2 = "1234";
int n1 = Integer.parseInt(s1);
int n2 = Integer.parseInt(s2);    
System.out.println(n1 == n2? "Integer.parseInt(s1) == Integer.parseInt(s2)":"Integer.parseInt(s1) != Integer.parseInt(s2)");
-------------------------
Integer.parseInt(s1) == Integer.parseInt(s2)

Integer.valueOf (s) usage

Integer.valueOf (s) to parse the string s type Integer object, the method returns the integer object can call.

String s = "1234";
Integer integer = Integer.valueOf(s);
System.out.println("integer : " + integer);
int n1 = Integer.parseInt(s1);
int n2 = Integer.parseInt(s2);    
System.out.println(n1 == n2? "Integer.parseInt(s1) == Integer.parseInt(s2)":"Integer.parseInt(s1) != Integer.parseInt(s2)");

--------------
1000
Integer.parseInt(s1) == Integer.parseInt(s2)

new Integer (s) Usage

Integer i = new Integer("1234");
Integer j = 1234;
System.out.print(i == j);  
-------------------
false

Since the java Integer variable points to the constant pool of objects,
the new new Integer () variable points to the new object in the heap, the two different addresses in memory.

Difference between the three

Integer.parseInt (s) a plurality of times the same basic types of data parsing int string obtained are equal, can be directly through the "==" to judge whether or not equal.

ps: int type is basic, does not contain the equals method, you can only use "==" comparison, the basic type of "==" is the size comparison of two values.

Integer.valueOf (s) when a multiple parsing the same string, to give the object is of type Integer, sometimes resulting object is the same object, the object may be different, according to the integer s of the string parsing sizes determined: the object type integer If the string s corresponding integer value between -128 and 127, is then parsed in the same object; s if the string between the integer value corresponding to -128 to 127 is not, then Object type Integer parsed not the same object.

ps: regardless of whether the object is equal, value the value of the object are equal.
Integer new new (S)  S string corresponding integer value between -128 and 127, is acquired from the constant pool may be directly; integer value S corresponding to the string need to open up a space between the heap memory is not stored in the -128 to 127 this value, when the value of the heap memory mapped to the address stack according to.

ps: When the character string corresponding to the integer s is 1000, not between -128 and 127, by Integer.valueOf (s), new Integer (s) the parsed objects i1 and i2 are two different objects, object the values ​​are the same value.

Three links

Integer.parseInt (s) is to parse the string into a substantially int type, Integer.valueOf (s) and new Integer (s) is to parse the string into object types Integer, Integer fact is unpacked int, int Integer packaging is in jdk8 has been automatically integer values ​​automatically unpacked and automatic packaging, so the two methods can get you want.

Conclusion

When we need it is a basic type i NT when we need to use the Integer.parseInt()function

When we need is an Integer object class of the time we just use Integer.valueOf()the function, new Integer () the way I personally do not like.

When we need to determine the value converted in the range -127 to 128, preferably the Integer.parseInt select (), of course, choose int Integer receiving and there is no relationship.

Guess you like

Origin www.cnblogs.com/JackpotHan/p/11264295.html