On the Integer class source Jdk1.8

 Look at its successor, realize the relationship:
public Integer the extends Number The Final class the implements the Comparable <Integer>

Number is an abstract class, comprising about six abstract methods are used to convert the type of the specific code as follows:
public abstract class Number the implements the java.io.Serializable {

public abstract int intValue ();

public abstract Long longValue in ();

public abstract o floatValue a float ();

public abstract doubleValue in Double ();

public byte o byteValue () {
return (byte) intValue ();
}

public Short shortValue () {
return (Short) intValue ();
}

/ ** use serialVersionUID from the JDK 1.0 Interoperability for * .2 /
Private Long serialVersionUID = Final static -8742448824652078965L;
}
        this is achieved following the Cheng Houzi class strong turn.
Long longValue in public (http://www.amjmh.com) {
return (Long) value;
}
        Comparable interface look, compare the size of only a compareTo methods:
public int compareTo (T O);    
            look Integer specific        
 public int compareTo (Integer anotherInteger) {
        return Compare (this.value, anotherInteger.value);
    }

    public static int Compare (int X, Y int) {
        return (X <Y) -1: ((X == Y) 0:?. 1);?
    }
      If you look at the code bit strenuous, then copy the following code, see the results of their run to understand a.
        
the Test class {public
public static void main (String [] args) {
Integer IT1 = 128;
Integer IT2 = -128;
Integer IT3 = 128;
System.out.println (it2.compareTo (IT1) + "----- X <Y ");
System.out.println (it1.compareTo (IT2) +" ------ X> Y ");
System.out.println(Integer.compare(it1,it3)+"-----x == y");

}
}

       Print Results:
-1 ----- X <Y
. 1 ------ X> Y
0 X == Y -----

 


2. Integer cache problems

            Integer default is placed in the cache interval -128 to 127. But the biggest cache value can be adjusted manually, see:

        The Javadoc comment clearly states that this class is for cache and to support the autoboxing of values between 128 and 127. The high value of 127 can be modified by using a VM argument -XX:AutoBoxCacheMax=size. So the caching happens in the for-loop. It just runs from the low to high and creates as many Integer instances and stores in an Integer array named cache. As simple as that. This caching is doing at the first usage of the Integer class. Henceforth, these cached instances are used instead of creating a new instance (during autoboxing).

         Run the following code to see results:
        
public class IntegerTest {
public static void main (String [] args) {
Integer IT1 = 2008;
Integer IT2 = 2008;
System.out.println (IT1 == IT2); // to true Note: Why over 127 of it was true? Vm arguments because I set the parameters -XX: AutoBoxCacheMax = 2008
Integer it1t = 2009;
Integer it2t = 2009;
System.out.println (it1t == it2t); // false Description: more than 2008 false, right ~ ~
Integer = newit1 Integer new new (127);
Integer = newit2 new new Integer (127);
System.out.println (newit1 == newit2); // to false Description: new words are independent memory occupied by a new object in the heap
int IT3 = 127;
int = 127 IT4;
int IT5 = 2008;
System.out.println (== IT4 IT3); to true //
System.out.println (IT1 == IT5); // to true
}
}
            Print Result:
        
to true
false
false
to true
to true

        set the virtual machine startup parameters
---------------------

Guess you like

Origin www.cnblogs.com/ly570/p/11257594.html