在Java中,打印一个字符串的地址值可以通过使用System.identityHashCode()方法实现。System.identityHashCode()方法返回指定对象的哈希码值,这个值在对象的生命周期中保持不变。在Java中,对象的地址值就是它的哈希码值。

下面是一个使用System.identityHashCode()方法打印字符串地址值的示例代码:

public class Main {
    public static void main(String[] args) {
        String str = "Hello, World!";
        int hashCode = System.identityHashCode(str);
        System.out.println("String address: " + hashCode);
    }
}

运行以上代码,将会输出类似以下结果:

String address: 366712642

在上述代码中,我们首先创建了一个字符串str,它的值是"Hello, World!"。然后,我们使用System.identityHashCode()方法获取str的哈希码值,并将其赋值给变量hashCode。最后,我们使用System.out.println()方法打印hashCode,即字符串的地址值。

需要注意的是,System.identityHashCode()方法返回的是一个整型值,它的范围可能超出了字符串表示的最大值。因此,在打印地址值时,我们可以将其转换为十六进制表示以更好地展示。

下面是一个修改后的示例代码,可以打印字符串地址值的十六进制表示:

public class Main {
    public static void main(String[] args) {
        String str = "Hello, World!";
        int hashCode = System.identityHashCode(str);
        String hexAddress = Integer.toHexString(hashCode);
        System.out.println("String address: 0x" + hexAddress);
    }
}

运行以上代码,将会输出类似以下结果:

String address: 0x15b85

在上述代码中,我们使用Integer.toHexString()方法将hashCode转换为十六进制表示,并将其赋值给变量hexAddress。然后,我们使用System.out.println()方法打印十六进制表示的字符串地址值。

通过以上代码,我们可以清晰地了解如何在Java中打印字符串的地址值,并可以根据需求选择打印十进制或十六进制表示的地址值。