2012-12-05 228 views
0

[email protected]Java:[email protected]

好的,f21代表软件包。 人类类型。

任何人都可以简单地解释为什么有一个“@”后跟随机字符。随机字符代表什么(在记忆中的位置?)。

我收到此当我做以下,并没有宣布一个toString()方法:

System.out.println(myObject); 

回答

3

如果你不覆盖在你的类的toString()方法,Object类的toString()会调用。

System.out.println(myObject);// this will call toString() by default. 

下面是java.Lang.Object类的toString的实施

 The {@code toString} method for class {@code Object} 
     returns a string consisting of the name of the class of which the 
     object is an instance, the at-sign character `{@code @}', and 
     the unsigned hexadecimal representation of the hash code of the 
     object 
public String toString() { 
    return getClass().getName() + "@" + Integer.toHexString(hashCode()); 
    } 

因此,应用相同的到[email protected]

21.Person(全类名)+ @ + 37ee92(该hasgcode的十六进制版本)

0

它调用toString()实现,如果你没有覆盖这个方法,那么它会调用Object的版本,其实现如下

public String toString() { 
    return getClass().getName() + "@" + Integer.toHexString(hashCode()); 
    } 

这是一个实例

0

如果不重写toString()方法,通过Object提供的一个用于的哈希码的十六进制版本。它the following

toString方法类Object返回字符串由其中的对象是一个实例,该符号字符@的类的名称,而散列码的无符号十六进制表示法的对象。换句话说,此方法返回一个字符串等于的值:

getClass().getName() + '@' + Integer.toHexString(hashCode()) 

“随机”字符是您的对象的哈希码,以十六进制。

相关问题