class A { } class Test extends A { public static void main( String[] args) {
A obj=new Test() ;
System.out.println(obj);
}
}
输出为: Test@de6cde ****************************************************************************************大概意思是,方法toString返回一个包含对象本身所属类的名字加上字符@
以及这个对象的哈希码(为十六进制形式)的字符串
toString
public String toString()
Returns a string representation of the object.
In general, the toString method returns a string that "textually represents" this object.
The result should be a concise but informative representation that is easy for a person to read.
It is recommended that all subclasses override this method.
The toString method for class Object returns a string consisting of the name of the class of which the object is an instance,
the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words,
this method returns a string equal to the value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())
Returns:
a string representation of the object.那么这个结果的物理意义是什么呢?
A obj=new Test() ;
System.out.println(obj);
}
}
输出为: Test@de6cde ****************************************************************************************大概意思是,方法toString返回一个包含对象本身所属类的名字加上字符@
以及这个对象的哈希码(为十六进制形式)的字符串
toString
public String toString()
Returns a string representation of the object.
In general, the toString method returns a string that "textually represents" this object.
The result should be a concise but informative representation that is easy for a person to read.
It is recommended that all subclasses override this method.
The toString method for class Object returns a string consisting of the name of the class of which the object is an instance,
the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words,
this method returns a string equal to the value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())
Returns:
a string representation of the object.那么这个结果的物理意义是什么呢?
解决方案 »
- JavaSE与JavaEE有什么区别
- 误区:大家都说StringBuilder在单线程下比StringBuffer效率高
- 送分求助一下个小问题,请各位高手来帮忙解决
- 如何把字符串转化为double 或是float 形
- 长春鸿达公司怎么样啊??面试难吗,请有过经历的人给小弟传授一些经验啊!!
- 请问有谁知道中软国际JAVA培训部的有关信息呀!
- 如何设置树状结构的外观?
- ?????
- 谁能告诉我在英文S275教材中或者说在java领域中 "block" 的准确翻译...............
- 楼下的大碗炒面才两块钱一碗,散分
- 关于 HttpServletRequest 接口的 getPathInfo() 方法
- java
[C —— char 一维数组
[D —— double 一维数组
[F —— float 一维数组
[I —— int 一维数组
[J —— long 一维数组
[L<classname>; —— 对象类型一维数组
[S —— short 一维数组
[Z —— boolean 一维数组比如:[[[C@xxxxxx 表示“三维的 char 数组”。具体的可以看看 The Java Virtual Machine Specification
其实这个值跟内存地址没有关系,这只是JVM内部用来管理对象用的一个唯一标识,两个对象的hashCode值相等标识JVM对它们的标识是相同的,并不一定代表内存地址就一致或者并不一定代表它们就是引用的同一个对象。我们都知道"=="在Java中是用来比较内存地址的运算,hashCode到底会怎么计算出来呢,是否是内存地址呢,看一个简单的例子就可以证明了:
String str1 = "hello";
String str2 = "hello";
String str3 = new String("hello");System.out.println(str1==str2); //print true
System.out.println(str1==str3); //print false
System.out.println(str1.hashCode()==str2.hashCode()); //print true
System.out.println(str1.hashCode()==str3.hashCode()); //print true
Java API Document关于hashCode方法的一句话说得很清楚:This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.就是说这个hashCode值通常来说是用这个对象的内存地址来计算出来的,但实现技术并不受限制,原理就像当初我学C++去取唯一随机数可以选择用系统时间做种子去计算一样。但由于它只是一个标识并且可以被重写,你想return多少都可以,所以不要误解了它就是内存地址。在Java中它唯一的作用是被一些Collection来标识是否包含重复元素用的。
呵呵,String 的 hashCode 方法被改写过的,采用每个字符进行计算的,只要两个字符串的内容相同,
那么它们的 hashCode 肯定是相同的。
其实追溯到native就没有定论了,虚拟机到底怎么计算它我还真不确定,拿内存地址当然也是最常见的做法之一
向你致敬!
String s2 = new String("hello,world");
这两者是有区别的,前者是在java对象池中取,后者是在堆中分配新空间。
所以
String s1 = "hello,world";
String s3 = "hello,world";s1 == s3 返回true;而
String s2 = new String("hello,world");
String s4 = new String("hello,world");s2 == s4 返回false;另:
对于数值类型,比如Integer, Java对象池只支持-128~127只见的数
例:
Integer i1 = 1;
Integer i2 = 1;
i1 == i2 返回true; Integer i3 = 287;
Integer i4 = 287;
i3 == i4 返回false;