JAvaベース——パッケージ類の面白いこと
JAvaのベースクラスにはそれぞれ対応するパッケージクラスがあります
基本データ型
パッケージクラス
byte
Byte
boolean
Boolean
short
Short
char
Char
int
Integer
long
Long
float
Float
double
Double
出力の結果
このような理由でIntegerのキャッシュメカニズムが形成されたことに関連して、java 5のIntegerの操作に新しい機能が導入され、メモリを節約し、パフォーマンスを向上させ、オブジェクト全体が同じアプリケーションオブジェクトを使用することでキャッシュと再利用を実現した.
逆コンパイルされたコード
コンパイルされたバイトコードここの自動箱詰めコンパイルされたバイトコードはIntegerのvalueof javaソースコードを呼び出したvalueofに関するものです
ここでは、まず、このiの値がキャッシュに規定された最大、最小の間にあるか否かを判断する値であり、確実に範囲内であればキャッシュ内の値を直接返し、そうでなければ、新たに1つのオブジェクトという値の範囲が-128から127の間である、すなわち、数値がこの間であればキャッシュ内の値を直接返すようにして「=」を用いると等しくなり、いずれも1つのハンドルを使用しているため、この範囲外、例えば300という数値でオブジェクトを再newすると、"=="を使用するとfalseの現象が発生します.
ソースコードおよびソースコードの解釈から、この-128から127までの数値範囲は変更可能であり、java.lang.Integer.IntegerCache.highパラメータを構成して彼の値を設定し、この値は127と比較して大きな値をとる.同時に、最大int値に128を加えて最小値をとり、その後、より小さい値をとる.最小値は変更不可であり、常に-127である.この値は変更インタフェースを提供していない.注記から-XX:AutoBoxCacheMax=これを構成することで変更できるので、パッケージクラスを比較する際にequal()というキャッシュ動作を使用するのはIntegerイメージだけではない.すべての整数タイプのクラスに対して同様のキャッシュメカニズムがあります.ByteCacheは、Byteオブジェクトをキャッシュするために使用され、ShortCacheは、Shortオブジェクトをキャッシュするために使用され、LongCacheは、Longオブジェクトをキャッシュするために使用され、CharacterCacheは、CharacterオブジェクトByteをキャッシュするために使用され、Shortは、Longは、一定の範囲を有する.Characterの場合、範囲は0~127です.Integer以外は、この範囲は変更できません.
基本データ型
パッケージクラス
byte
Byte
boolean
Boolean
short
Short
char
Char
int
Integer
long
Long
float
Float
double
Double
public static void main(String[] args) {
Integer value1=100;
Integer value2 = 100;
System.out.println("value1==value2 "+(value1==value2));
Integer value3=300;
Integer value4=300;
System.out.println("value3==value4 "+(value3==value4));
}
出力の結果
value1==value2 true
value3==value4 false
このような理由でIntegerのキャッシュメカニズムが形成されたことに関連して、java 5のIntegerの操作に新しい機能が導入され、メモリを節約し、パフォーマンスを向上させ、オブジェクト全体が同じアプリケーションオブジェクトを使用することでキャッシュと再利用を実現した.
逆コンパイルされたコード
0: bipush 100
2: invokestatic #2 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
5: astore_1
6: bipush 100
8: invokestatic #2 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
11: astore_2
12: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
15: new #4 // class java/lang/StringBuilder
18: dup
19: invokespecial #5 // Method java/lang/StringBuilder."":()V
22: ldc #6 // String value1==value2
24: invokevirtual #7 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
コンパイルされたバイトコードここの自動箱詰めコンパイルされたバイトコードはIntegerのvalueof javaソースコードを呼び出したvalueofに関するものです
/**
* Returns an {@code Integer} instance representing the specified
* {@code int} value. If a new {@code Integer} instance is not
* required, this method should generally be used in preference to
* the constructor {@link #Integer(int)}, as this method is likely
* to yield significantly better space and time performance by
* caching frequently requested values.
*
* This method will always cache values in the range -128 to 127,
* inclusive, and may cache other values outside of this range.
*
* @param i an {@code int} value.
* @return an {@code Integer} instance representing {@code i}.
* @since 1.5
*/
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
ここでは、まず、このiの値がキャッシュに規定された最大、最小の間にあるか否かを判断する値であり、確実に範囲内であればキャッシュ内の値を直接返し、そうでなければ、新たに1つのオブジェクトという値の範囲が-128から127の間である、すなわち、数値がこの間であればキャッシュ内の値を直接返すようにして「=」を用いると等しくなり、いずれも1つのハンドルを使用しているため、この範囲外、例えば300という数値でオブジェクトを再newすると、"=="を使用するとfalseの現象が発生します.
/**
* Cache to support the object identity semantics of autoboxing for values between
* -128 and 127 (inclusive) as required by JLS.
*
* The cache is initialized on first usage. The size of the cache
* may be controlled by the {@code -XX:AutoBoxCacheMax=} option.
* During VM initialization, java.lang.Integer.IntegerCache.high property
* may be set and saved in the private system properties in the
* sun.misc.VM class.
*/
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) {
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];
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() {}
}
ソースコードおよびソースコードの解釈から、この-128から127までの数値範囲は変更可能であり、java.lang.Integer.IntegerCache.highパラメータを構成して彼の値を設定し、この値は127と比較して大きな値をとる.同時に、最大int値に128を加えて最小値をとり、その後、より小さい値をとる.最小値は変更不可であり、常に-127である.この値は変更インタフェースを提供していない.注記から-XX:AutoBoxCacheMax=これを構成することで変更できるので、パッケージクラスを比較する際にequal()というキャッシュ動作を使用するのはIntegerイメージだけではない.すべての整数タイプのクラスに対して同様のキャッシュメカニズムがあります.ByteCacheは、Byteオブジェクトをキャッシュするために使用され、ShortCacheは、Shortオブジェクトをキャッシュするために使用され、LongCacheは、Longオブジェクトをキャッシュするために使用され、CharacterCacheは、CharacterオブジェクトByteをキャッシュするために使用され、Shortは、Longは、一定の範囲を有する.Characterの場合、範囲は0~127です.Integer以外は、この範囲は変更できません.