×
データ型と変数
参照型変数とヒープ領域上のオブジェクト実体の関係を示すコード例です。
public class ReferenceExample {
    public static void main(String[] args) {
        String str1 = new String("Hello");
        String str2 = str1; // アドレス(参照)のコピー
        
        System.out.println(str1 == str2); // true (同じオブジェクトを参照)
        
        str1 = null; // str1は参照を失う
        // System.out.println(str1.length()); // NullPointerExceptionが発生!
    }
}

もう少し詳しく