2017-11-04 20:39:26
Basic Type Encapsulation Classes : The benefit of encapsulating classes of basic types is that you can define more functional methods in the object to manipulate the data.
One of the common operations: conversions for basic data types and strings.
Corresponding to the basic type and wrapper class:
byte byte,short short,int integer,long long,float float,double Double,char Character,boolean Boolean.
The following is an example of an integer:
Integer:IntegerClass wraps a value of a primitive type in an object int . An Integer object of type contains a int field of type.
In addition, the class provides several methods that can be int String converted between types and types, as well as int other constants and methods that are useful when handling types.
* Construction Method
* Common Properties
* Common Methods
- Cross-transfer of string and int
int to string string S1 = string.valueof (+); System.out.println (S1); string to int int i=integer.parseint (S1); System.out.println (i);
Decimal turn binary, octal, hex System.out.println (integer.tobinarystring); System.out.println (Integer.tooctalstring ()); System.out.println (Integer.tohexstring ()); Decimal to any binary, the binary range is [2,36], because there are only 36 0-9,a-z. System.out.println (integer.tostring (100,16)); Other binary decimal //binary 100 to decimal System.out.println (integer.parseint ("100", 2));
- JDK5 new features: Automatic unboxing
Auto boxed Integer i=100; Automatic unpacking i+=200; System.out.println (i); The code //Integer i=integer.valueof (+) of the automatic disassembly box can be obtained by anti-compilation; I=integer.valueof (I.intvalue () +200); System.out.println (string.valueof (i));
[note]:The integer object cannot be null. Otherwise, NullPointerException will appear. It is recommended that you first determine whether NULL is used.
Integer i1 = new Integer (127); Integer i2 = new Integer (127); System.out.println (I1==I2); False System.out.println (I1.equals (I2)); True integer i3 = new Integer (+); Integer i4 = new Integer (+); System.out.println (I3==I4); False System.out.println (I3.equals (I4)); True Integer i5 = 127; Integer I6 = 127; System.out.println (I5==I6); True System.out.println (I5.equals (I6)); True Integer i7 = n; Integer i8 = n; System.out.println (I7==I8); False System.out.println (I7.equals (i8)); False
In the object comparison, "= =" is the comparison memory address, and the Equals () function is the comparison address by default, but here the integer class overrides the method, so is the comparison of the value size, therefore, all equals is the same.
Obviously, the address of the new object must be different, so the first two are false when judging "= =".
So why is there a difference after automatic boxing?
From the above source analysis we know that automatic boxing at the bottom is actually called the valueof (int i) method to convert the int type to an integer type. The source code of valueof () is as follows:
static final int low = -128;static Final int high;static {//High value M AY is configured by property int h = 127; String Integercachehighpropvalue = Vm.getsavedproperty ("Java.lang.Integer.IntegerCache.high"); if (integercachehighpropvalue! = null) {try {int i = parseint (Integercachehighpropvalu e); i = Math.max (i, 127); Maximum array size is integer.max_value h = math.min (i, Integer.max_value-(-low)-1); } catch (NumberFormatException nfe) {//If The property cannot is parsed into an int, ignore it. }}high = h; public static Integer valueOf (int i) {if (I >= integercache.low && i <= integercache.high) return integercache.cache[i + (-integercache.low)]; return new Integer (i); }
That is, the valueof method is treated differently when called, and the number of [-128,127] is taken directly from the buffer pool, and the data that is not in that range is returned by a new object.
ValueOf: The return is a wrapper class
parseint: The basic data type is returned
Java common objects-basic types of encapsulation classes