Java 深入学习(6) —— 打印 String 对象引用时显示的不是 hashCode 而是 String 对象本身的原因
来源:互联网 发布:php集成开发包 编辑:程序博客网 时间:2024/06/06 07:01
问题概述
正常打印一个 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("------------------------------"); }}
truefalsefalse------------------------------com.practice.string.StringCannotChange@73com.practice.string.StringCannotChange@73com.practice.string.StringCannotChange@73com.practice.string.StringCannotChange@73------------------------------truetruetrue------------------------------com.practice.string.StringCannotChange@4554617c------------------------------test string------------------------------Process finished with exit code 0
Reference
https://stackoverflow.com/questions/21964606/java-string-pool-storage-doubts
阅读全文
0 0
- Java 深入学习(6) —— 打印 String 对象引用时显示的不是 hashCode 而是 String 对象本身的原因
- java传递是引用的拷贝,既不是引用本身,更不是对象
- java传递是引用的拷贝,既不是引用本身,更不是对象
- java传递是引用的拷贝,既不是引用本身,更不是对象
- java String对象是“引用”传递的
- Java中Object对象的hashCode方法和String对象的hashCode
- 关于String对象的hashCode问题
- Java String类对象引用、字符串常量和String Pool 相互关系的学习
- String对象的学习
- java的string对象
- Java的String对象
- java对象引用和值引用,特殊的String!
- JAVA随笔篇二(深入分析JAVA简单类型、String和对象的值传递和引用传递)
- 为什么打印String对象,不是地址值?
- 学习笔记—JavaScript的String对象
- java string的hashcode
- String的hashcode(java)
- String的hashcode(java)
- 扩展jquery 实现增强版console.log方法
- 自顶向下 逐步求精”的方法
- firefox与IE的nextSibling
- jQuery对象与dom对象的转换
- 基础练习 高精度加法
- Java 深入学习(6) —— 打印 String 对象引用时显示的不是 hashCode 而是 String 对象本身的原因
- 2017/11/29 C#开发的几个问题/注意事项
- 重载浏览器的默认行为
- SQL 字符串拼接
- p2p技术之tcp内网穿透 java实现版
- 绑定事件监听函数
- MyEclipse2013 Hibernate反向工程步骤及DAO Type无法选择Spring DAO解决方法
- Android 获取本地存储路径的各种方法详解
- 在Spring中配置Quartz