在java中打印对象时会发生什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20735132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:55:42  来源:igfitidea点击:

What happens when printing an object in java

javaobject

提问by Marjer

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

我运行了上面的代码,我得到的结果为“data@1ae73783”System.out.println(dObj);和“5”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.

然后我进行了调试以检查打印对象时真正发生了什么,在调试模式下调用了很多参数(如类加载器,theards)
我知道第一次打印值代表类名,然后是地址。但是不知道在调试模式下到底发生了什么,因为第二次打印仅在调试模式下发生了变量赋值,即 b=5。

Please explain whats really happening?

请解释一下到底发生了什么?

回答by Thorn

All objects inherit from java.lang.Object which has a default implementation of toString. If an object overrides this method then out.print (obj) will put something useful on the screen.

所有对象都继承自 java.lang.Object,它具有 toString 的默认实现。如果一个对象覆盖了这个方法,那么 out.print (obj) 会在屏幕上放一些有用的东西。

Primitive data types are handled by a different, much simpler implementation of println. The println method is overridden for every data type in addition to Object.

原始数据类型由一个不同的、更简单的 println 实现处理。除了 Object 之外的所有数据类型都覆盖了 println 方法。

回答by MGorgon

First, intisn't an Object. It's primitive type. Second, when Class haven't overrived toString()method, toString()from Object class is invoked.

首先,int不是Object. 它是原始类型。其次,当 Class 没有覆盖toString()方法时,toString()从 Object 类被调用。

回答by nhgrif

Please explain whats really happening?

请解释一下到底发生了什么?

As other have told you, using System.out.printlnwith an object will call to toStringmethod on that object. If the class doesn't have it's own toStringmethod, then it's a call to the super class's toString. If the super class call goes all the way back to java.lang.Object, the default toStringmethod prints the name of the object's type (what class it is), followed by an @sign, and the memory location of the object--the hexidecimal address of where that object is stored in memory.

正如其他人告诉您的那样,System.out.println与对象一起使用将调用toString该对象上的方法。如果该类没有自己的toString方法,则它是对超类的toString. 如果超类调用一直回到java.lang.Object,默认toString方法打印对象类型的名称(它是什么类),后跟一个@符号,以及对象的内存位置——该对象所在位置的十六进制地址存储在内存中。

ClassName@MemoryLocation

回答by JB Nizet

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

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

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

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

The javadoc of String.valueOf(Object)says:

String.valueOf(Object)的 javadoc说:

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

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

And the javadoc of Object.toString()says:

Object.toString()的 javadoc说:

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:

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

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

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

回答by John Smith

data dObj = new data() does not exist in the source code; you want to print the string value of the object (Data), you have to override the toString method;

data dObj = new data() 在源代码中不存在;要打印对象(Data)的字符串值,必须重写 toString 方法;

try this

尝试这个

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

class Data {
  int a = 5;

  @Override
  public String toString() {
    return String.valueOf(a);
  }
}

回答by Raj Kumar Paswan

when we print object of any class System.out.print() gives string of class name along with memory address of object (ClassName@MemoryAdress)

当我们打印任何类 System.out.print() 的对象时,会给出类名字符串以及对象的内存地址 (ClassName@MemoryAdress)

回答by Kms

In Addition to @JB Nizet answer,

除了@JB Nizet 的回答,

To Provide our own string representation, we have to override toString() in our class which is highly recommended because

为了提供我们自己的字符串表示,我们必须在我们的类中重写 toString() ,这是强烈推荐的,因为

There are some classes in which toString() is overridden alreadyto get the proper string representation.

有一些类已经覆盖toString()以获得正确的字符串表示形式。

Examle:String, StringBuffer, StringBuilderand all wrapperclasses

示例:StringStringBufferStringBuilder和所有包装

    Integer ob1 = new Integer("10");
    String ob2 = new String("Doltan Roy");
    StringBuffer ob3 = new StringBuffer("The Rock");
    StringBuilder ob4 = new StringBuilder("The Joshua");

    System.out.println(ob1);
    System.out.println(ob2);
    System.out.println(ob3);
    System.out.println(ob4);

Output:

输出:

10

10

Doltan Roy

多尔坦·罗伊

The Rock

岩石

The Joshua

约书亚记

Hope this would help!

希望这会有所帮助!