本文介绍了在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);




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



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

解决方案

您不需要调试器就能知道发生了什么。 的类型为PrintStream。 说:

说:

说:



  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:

The javadoc of String.valueOf(Object) says:

And the javadoc of Object.toString() says:

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

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

08-20 06:20
查看更多