System.out.println(对象)的输出 [英] Output of System.out.println(object)

查看:242
本文介绍了System.out.println(对象)的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道当我执行以下操作时输出到底是什么。

I want to know what exactly the output is when I do the following.

class Data {
  int a = 5;
}

class Main {
  public static void main(String[] args) {
    data dObj = new data();
    System.out.println(dObj);
  }
}

我知道它提供了与对象相关的东西作为输出在我的情况下是 data @ 1ae73783 。我猜 1ae73783 是十六进制数。我也做了一些工作并打印

I know it gives something related to object as the output in my case is data@1ae73783. I guess the 1ae73783 is a hex number. I also did some work around and printed

System.out.println(dObj.hashCode());

我得到了数字 415360643 。我有一个整数值。我不知道 hashCode()返回,仍然出于好奇,当我将 1ae73783 转换为十进制时,我得到 415360643

I got the number 415360643. I got an integer value. I don't know what hashCode() returns, still out of curiosity, when I converted 1ae73783 to decimal, I got 415360643!

这就是为什么我很好奇这个数字到底是什么。这是Java的沙箱或其他东西的一些内存位置吗?

That's why I am curious about what exactly is this number. Is this some memory location of Java's sandbox or some other thing?

推荐答案

默认情况下, toString() 方法正在被使用。此方法定义如下:

What happens is that the default toString() method of your class is getting used. This method is defined as follows:


类<$ c的 toString 方法$ c> Object 返回一个字符串,该字符串由对象为实例的类的名称,at符号字符@以及对象的哈希码的无符号十六进制表示形式组成。 。换句话说,此方法返回一个等于值的字符串:

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())


返回的值默认 hashCode() 方法是特定于实现的:

The value returned by the default hashCode() method is implementation-specific:


尽可能合理实用,类 Object 定义的 hashCode 方法确实为不同的对象返回不同的整数。 (这通常通过将对象的内部地址转换为整数来实现,但JavaTM编程语言不需要此实现技术。)

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (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.)

这篇关于System.out.println(对象)的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆