問1
「Sample.java」のソース
public class Sample {
public static void main(String[] args) {
// declaration.
int primitiveFirst;
int primitiveSecond;
Integer referenceFirst;
Integer referenceSecond;
// definition.
primitiveFirst=100;
primitiveSecond=100;
referenceFirst=new Integer(100);
referenceSecond=new Integer(100);
// comparison.
System.out.print("Result which compared"
+" \'primitiveFirst\' with \'primitiveSecond\'");
System.out.println(" : "+(primitiveFirst==primitiveSecond));
// -
System.out.print("Result which compared"
+" \'referenceFirst\' with \'referenceSecond\'");
System.out.println(" : "+(referenceFirst==referenceSecond));
}
}
|
上のプログラムを実行すると,次の結果が得られます.
実行結果
Result which compared 'primitiveFirst' with 'primitiveSecond' : true
Result which compared 'referenceFirst' with 'referenceSecond' : false
|
同じ数値(100)なのに,どうして結果が異なるか わかりますか?
この2つの違いは何でしょう?primitiveFirst==primitiveSecond referenceFirst==referenceSecond