This article turns from http://blog.csdn.net/springcsc1982/article/details/8788345 thanks to the author
A test program was written, as follows:
int a = 1000, b=;
System.out.println (A = = B);
Integer C = N, d = 1000;
System.out.println (c = = d);
Integer e = 100,h = 100;
System.out.println (E = = h);
Come to an astonishing result, the result is: True,false,true, the first statement to say, compare is the value, so the result is true, the second and the third, the difference is only the number of different, why will come to different results? Then look at the source code to find the answer, originally, all the number classes have a caching mechanism, as long as the number is 128 to 127 will be cached, in this range of data, the new object will be generated.
As the results of the program run are rather strange, the path to finding answers has begun, and the main classes involved include:
Java.lang.Integer, Java.lang.Number, and because number is the parent of all numeric classes, learn all its related classes again,
Including
Java.lang.Byte,
Java.lang.Double,
Java.lang.Float,
Java.lang.Long,
Java.lang.Short
The above types of relationships are as follows:
As you can see, these data classes are inherited from the number class, and number is an abstract class that contains a variety of methods for converting data into various types of data:
public abstract int intvalue ();
Public abstract long Longvalue ();
public abstract float floatvalue ();
public abstract double Doublevalue ();
Public byte Bytevalue ()
public short Shortvalue ()
We open the integer to analyze:
private Static Class integercache{
Private Integercache () {}
Static Final Integer cache[] = new integer[-(-128) + 127 + 1];
Static {//Static code block, which is called when the virtual machine mounts the class
for (int i = 0; i < cache. length; i++)
Cache [I] = new Integer (i-128);
}
}
Public Static Integer valueOf (int i) {
final int offset = 128;
if (I >= -128 && i <= 127) {//must cache
return Integercache. Cache [i + offset];//returns the object in the cache
}
return new Integer (i);//Generate New Object
}
The integer class is found to include an inner class in which a cache array is defined, a cache is generated in a static code block, and 256 integer objects are stored. When the passed-in value is between 128 and 127, the system automatically acquires the object in the cache, and when it is not in this interval, the system generates a new object, so the code is executed as an integer c = n, d = 1000; System.out.println (c== D); When the system automatically generates two new objects, the natural result is false, and when a=100,b=100, the printed result is true.
Similarly, Long, Float, Double, Byte, short also exist in the internal cache class Longcache, Floatcache, Doublecache, Bytecache, Shortcache.
Let's take a look at some of the properties and methods of the integer class:
Size property: That is, the number of bits, integer is 32 bits, double is 64 bits, long is also 64 bits, float is 32 bits, short is 16 bits, byte is 8 bits
Public static Final class<integer> TYPE = (class<integer>) Class. Getprimitiveclass ("int");
Class. Getprimitiveclass ("int"): Gets the type int of the virtual machine level, stating that integer is different from int, and int is the type inside the virtual machine, and the integer is simply encapsulated on this type.
toString (int i, int radix) converts a number to a string based on the radix binary
Tohexstring according to 16 binary output
Tooctalstring according to 8 binary output
tobinarystring in binary output
Stringsize returns the number of x digits in decimal
parseint (string s, int radix) converts a string to an X-binary integer
parseint (String s) defaults to decimal integers
ValueOf (string s, int radix) converts a string to an X-binary integer, but returns an integer when new
ValueOf (String s,) defaults to a decimal integer, but returns when new has an integer
Equals overrides the object, judging whether the value is the same, rather than judging
Getinteger returns the numeric value of the system attribute, this method seems to have a problem
decode resolves to an integer type based on string, the string may be X-binary
CompareTo compares the size of two values and returns 0,1,-1
After reading the integer class, the other classes are basically the same, and the other classes are related to the following methods:
Byte class, corresponding to the virtual machine's byte class
Bytecache is between 128 and 127.
Parsebyte conversion to byte type by X-binary
The valueOf is converted to a byte type according to the X-binary, and a new byte
public static byte decode (string nm) converted to byte from string
CompareTo comparison, and returns the difference of two values
Double class that corresponds to a double of the virtual machine
SIZE=64 64 bits, or 8 bytes
Isinfinite is infinitely large and infinitely small
IsNaN determine if two values are equal
Doubletolongbits long and double are 64 bits, this function converts a double to long because in Java, double cannot do bitwise operations
Longbitstodouble native method, it should be bit to double
Float class, equivalent to the float 32 bits of the virtual machine
Floattorawintbits native method, float to int
Longcache, Cache class
Highestonebit returns the highest one of a long value
Lowestonebit returns the lowest one of a long value
Bitcount returns the number of 1 bits in the binary complement representation of a specified Long value
Short is equivalent to a short 16-bit, two-byte inside the virtual machine
Basic data Type analysis----Java.lang.Number class and its subclass analysis