在java中打印对象时会发生什么 [英] What happens when printing an object in java

查看:132
本文介绍了在java中打印对象时会发生什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  class Data {
int a = 5;
}

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

我想知道打印对象时发生了什么或数字或字符串。



我运行了上面的代码,我得到的结果是$ @ c $ c> System.out.println的数据@ 1ae73783( dObj); 和5System.out.println(b);



<然后我做了调试来检查打印对象时真的发生了什么,在调试模式中调用了很多参数(比如classloader,theards)

我知道第一次打印时值表示类名接着是地址。但是不知道在调试模式下真正发生了什么,因为第二次打印只在调试模式中发生了变量赋值,即b = 5.



请解释一下真的发生了什么?

解决方案

您不需要调试器就能知道发生了什么。 System.out 的类型为PrintStream。 PrintStream.println(Object)的javadoc )说:


打印一个对象,然后终止该行。此方法首先调用String.valueOf(x)来获取打印对象的字符串值,然后表现为调用print(String)然后调用println()。


String.valueOf(Object)说:


如果参数为null,则字符串等于null ;否则,返回obj.toString()的值。


Object.toString()说:


返回对象的字符串表示形式。通常,toString方法返回一个文本表示此对象的字符串。结果应该是简洁但信息丰富的表示,便于人们阅读。建议所有子类重写此方法。



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




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


class Data {
    int a = 5;
}

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

I want to know what's happening when printing a object or number or string.

I ran the above code, i'm getting the result as "data@1ae73783" for System.out.println(dObj); and "5" for System.out.println(b);

Then I did debug to check whats really happening when printing a object, there was lot of parameter called in a debug mode(like classloader,theards)
I know for the first print the value represent class name followed by address. But don't know what's really happening in debug mode, for the 2nd print only variable assignment happened in the debug mode i.e b=5.

Please explain whats really happening?

解决方案

You don't need a debugger to know what's happening. System.out is of type PrintStream. The javadoc of PrintStream.println(Object) says:

Prints an Object and then terminate the line. This method calls at first String.valueOf(x) to get the printed object's string value, then behaves as though it invokes print(String) and then println().

The javadoc of String.valueOf(Object) says:

if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned.

And the javadoc of Object.toString() says:

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

这篇关于在java中打印对象时会发生什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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