public class test { public static void main(String [] args) { ab a = new ab(); System.out.println(a); } }class ab { public void say() { System.out.print("Hello!"); } }
你要的那东东 在String 对象中是得不到的, 用一个普通java对象就可以得到 比如: class A{} class B{ public static void main(String args[]) { System.out.println(new A()); } } 普通对象的这个地址是通过他的toString()方法得到的,而String类重写了toString() 。
楼主:我看 Recursion@7d772e 也不是地址。
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:
楼上的,呵呵:) public native int hashCode(); Object源码中这个函数并没有实现,而在String中做了下面的 public int hashCode() { int h = hash; if (h == 0) { int off = offset; char val[] = value; int len = count; for (int i = 0; i < len; i++) { h = 31*h + val[off++]; } hash = h; } return h; } 但是在所覆写的toString()中 public String toString() { return this; } 可见在类String中toString()与hashCode()并没有什么联系
2、 String str2=null;
3、 str1 = new Stirng("abc");
4、 str2 = str1;当程序执行到第三条语句时,JVM 就在堆内存里生成一个String对象,然后将它的首地址分给栈内里的str1 。 执行第四句时,就是将str1 的值(即刚才生成那个对象的首地址)赋给str2。
{
public static void main(String [] args)
{
ab a = new ab();
System.out.println(a);
}
}class ab
{
public void say()
{
System.out.print("Hello!");
}
}
用一个普通java对象就可以得到
比如:
class A{}
class B{
public static void main(String args[])
{
System.out.println(new A());
}
}
普通对象的这个地址是通过他的toString()方法得到的,而String类重写了toString() 。
public native int hashCode();
Object源码中这个函数并没有实现,而在String中做了下面的
public int hashCode() {
int h = hash;
if (h == 0) {
int off = offset;
char val[] = value;
int len = count; for (int i = 0; i < len; i++) {
h = 31*h + val[off++];
}
hash = h;
}
return h;
}
但是在所覆写的toString()中
public String toString() {
return this;
}
可见在类String中toString()与hashCode()并没有什么联系
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
才真正和hashCode()相关