Java 深入学习(6) —— 打印 String 对象引用时显示的不是 hashCode 而是 String 对象本身的原因

问题概述


正常打印一个 Object 对象引用的时候,会默认打印 Object.toString() 方法返回的 getClass().getName() + "@" + Integer.toHexString(hashCode())

即打印出 对象所属类的名字与对象的 hashCode

StringCannotChange stringCannotChange = new StringCannotChange();
System.out.print(stringCannotChange);
com.practice.string.StringCannotChange@4554617c

但是当打印 String对象的引用时,则会显示字符串本身。

String str = "test string";
System.out.println(str);
test string

原因是 String 重写了 toString() 方法


Object.java 源码

这里写图片描述

String.java 源码

这里写图片描述


测试代码


package com.practice.string;

public class StringCannotChange {
    public static void main(String[] args) {
        // s 是对 常量存储区 中 "s" 的引用
        String s = "s";

        // s2 是对 常量存储区 中 "s" 的引用
        String s2 = "s";

        // s3(存储于内存的栈区,指向了新对象的首地址) 是对 一个新对象(存储于内存的堆区)的 引用
        String s3 = new String("s");

        // s4 也是一个对新对象的引用,但和 s3 分别指向不同的对象
        String s4 = new String("s");

        System.out.println(s == s2);  // true
        System.out.println(s3 == s2); // false
        System.out.println(s3 == s4); // false
        System.out.println("------------------------------");
        /*
        * A String's hashCode() method is calculated based on the chars it contains.
        * If two String objects contain the same chars with the same case and in the same order,
        * then they will have the same hashCode().
        * */
        System.out.println(StringCannotChange.class.getName() + "@" + Integer.toHexString(s.hashCode()));
        System.out.println(StringCannotChange.class.getName() + "@" + Integer.toHexString(s2.hashCode()));
        System.out.println(StringCannotChange.class.getName() + "@" + Integer.toHexString(s3.hashCode()));
        System.out.println(StringCannotChange.class.getName() + "@" + Integer.toHexString(s4.hashCode()));
        System.out.println("------------------------------");

        System.out.println(s.equals(s2));   // true
        System.out.println(s.equals(s3));   // true
        System.out.println(s.equals(s4));   // true
        System.out.println("------------------------------");

        StringCannotChange stringCannotChange = new StringCannotChange();
        System.out.print(stringCannotChange);
        System.out.println("------------------------------");

        String str = "test string";
        System.out.println(str);
        System.out.println("------------------------------");

    }
}


true
false
false
------------------------------
com.practice.string.StringCannotChange@73
com.practice.string.StringCannotChange@73
com.practice.string.StringCannotChange@73
com.practice.string.StringCannotChange@73
------------------------------
true
true
true
------------------------------
com.practice.string.StringCannotChange@4554617c------------------------------
test string
------------------------------

Process finished with exit code 0

Reference

https://stackoverflow.com/questions/21964606/java-string-pool-storage-doubts

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值