整数と整数の比較と違い



Comparison Difference Between Integer



整数と整数の比較と違い


タイトルなど: Integer a = new Integer(3) Integer b = 3 int c = 3 System.out.println(a == b) System.out.println(a == c) Integer f1 = 100, f2 = 100 Integer f3 = 200 Integer f4 = 200 System.out.println(f1 == f2) System.out.println(f3 == f4) System.out.println(f3 == (f1 + f2))

結果:
a == bの答えは次のとおりです。false
いずれの場合も、整数と新しい整数は等しくなりません。ボックス化解除プロセスを経ず、整数の参照はヒープを指し、新しい整数は彼のメモリ(定数プール)を格納します。それらのメモリアドレスは同じではないため、falseです。

オブジェクトに対する「==」の判断は、2つのオブジェクトのアドレスが等しいかどうかに基づいているため、理由は非常に単純です。明らかに、aは新しく作成されたオブジェクトであり、bによって自動的にラップされたヒープは同じアドレスを指していないため、Falseです。



a == cの答えは次のとおりです。true
ラッパークラスIntegerと基本型データは、比較のために自動的にボックス化されません(IntegerのintValue()メソッドを呼び出します)。
整数は自動的にintとしてボックス化されてから比較されるため、intとinteger(新しいnoに関係なく)の比率はtrueです。

cは基本型のデータであるため、ラッパークラスIntegerと基本型データは比較時に自動的にボックス化されません(IntegerのintValue()メソッドを呼び出します)。つまり、オブジェクトの値と基本型cを値Comparisonに使用します。 。



f1 == f2の答えは次のとおりです。true
f3 == f4の答え:false
どちらも新しい整数ではありません。数値が-128〜127の場合はtrue、それ以外の場合はfalseです。

javaがIntegerx = 1をコンパイルすると、-> Integer x = Integer.valueOf(1)に変換されます。
JDKソースコードのvalueOf関数は次のようになります。

public static Integer valueOf(int i) { assert IntegerCache.high >= 127 if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)] / / Determine whether it is in the cache array, if it is, return directly return new Integer(i)/ / Not in the cached array, directly new a new object returned }

IntegerCacheクラスを見てください。



private static class IntegerCache { static final int low = -128 static final int high static final Integer cache[] static { // high value may be configured by property int h = 127 String integerCacheHighPropValue = sun.misc.VM.getSavedProperty('java.lang.Integer.IntegerCache.high') if (integerCacheHighPropValue != null) {/ / Check the virtual machine for the corresponding configuration, if you take the value and the default value of the maximum if not take the default value try { int i = parseInt(integerCacheHighPropValue) 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 be parsed into an int, ignore it. } } high = h cache = new Integer[(high - low) + 1]/ / Create a cache array and initialize the value of the array int j = low for(int k = 0 k < cache.length k++) cache[k] = new Integer(j++) // range [-128, 127] must be interned (JLS7 5.1.7) assert IntegerCache.high >= 127 } private IntegerCache() {} }

これは、Integerクラス内でのみアクセスできる内部静的クラスです。このクラスは、初期化時にJVM構成をロードします。値がある場合は、構成された値でキャッシュ配列を初期化します。そうでない場合は、キャッシュされます-128〜127の値。

f3 ==(f1 + f2)の答え:true
シンボリック演算(IntegerのintValue()メソッドを呼び出す)を実行すると、整数オブジェクトは自動的にボックスから解放されます。

シンボリック演算(IntegerのintValue()メソッドを呼び出す)を実行すると、Integerオブジェクトは自動的にボックス化されないため、f1 + f2はそのオブジェクト値の合計であり、結果はint型の200になります。 f3と比較すると、200はint型の基本型データであるため、f3も自動的にボックス化されません。前の操作で取得した200と比較すると、値は等しく、結果はtrueです。

コンパイルされたバイトコードファイル(逆コンパイルされたもの)は以下に添付されています:

Integer a = new Integer(3) Integer b = Integer.valueOf(3) int c = 3 System.out.println(a == b) System.out.println(a.intValue() == c) Integer f1 = Integer.valueOf(100) Integer f2 = Integer.valueOf(100) Integer f3 = Integer.valueOf(200)////This is equivalent to two new objects, == compares the memory address of the reference variable, so it is false Integer f4 = Integer.valueOf(200) System.out.println(f1 == f2) System.out.println(f3 == f4) System.out.println(f3.intValue() == f1.intValue() + f2.intValue())

両方の整数オブジェクトは新しく、falseです