java中“对象”的默认值是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20571675/
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 02:52:08  来源:igfitidea点击:

What's the default value for an 'object' in java?

javaobjectdefault-value

提问by

Out of blue, I came across this:

出乎意料,我遇到了这个:

public class demo {
    void multiply(){
       System.out.println("HELLO WORLD!")
       }
}

public static void main(String args[]){
        demo e=new demo();
        demo e1=new demo();
        System.out.println(e);
        System.out.println(e1);
    }
}

The weird output that I got when I executed the code was:

我执行代码时得到的奇怪输出是:

demo@6e1408

demo@e53108

演示@6e1408

演示@e53108

Or

或者

demo@1888759

demo@6e1408

演示@1888759

演示@6e1408

Can someone please explain to me what's happening? The value I got, is this a default value for an object, or am I missing something?

有人可以向我解释发生了什么吗?我得到的值,这是一个对象的默认值,还是我遗漏了什么?

采纳答案by Suresh Atta

You have to overridethe toString()method in your classes which you are printing.

您必须使用正在打印的类中overridetoString()方法。

Right now it's printing the default implementation of Objectclass toString()method.

现在它正在打印ObjecttoString()方法的默认实现。

From Object class Source code

来自对象类源代码

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

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

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

Returns:
a string representation of the object.


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

So in your Demo class Ovveride toString()method to get desired O/P.

因此,在您的 Demo 类中使用 OvveridetoString()方法来获得所需的 O/P。

    public class Demo{

    ----

       @Override
       public String toString() {
             //return something 
       }    
}

回答by Jigar Joshi

It is the toString()returned value you are seeing

这是toString()您看到的返回值

default Object's implementation

默认对象的实现

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

there is no default value for an Objectbut its member variable has default value based on its type

an 没有默认值,Object但其成员变量具有基于其类型的默认值

回答by M21B8

The value you see is the Objects address in memory, the fact the values are different shows it is not the same object. If you want to change what comes out, override the .toString() method.

您看到的值是内存中的对象地址,值不同的事实表明它不是同一个对象。如果你想改变结果,覆盖 .toString() 方法。

回答by stinepike

actually by default a object's toStringmethod returns someObjectClassname@hashcodenumber. to print your desired output you have to override the toStringmethod

实际上默认情况下,对象的toString方法返回someObjectClassname@hashcodenumber. 要打印所需的输出,您必须覆盖该toString方法

回答by Anthony Benavente

This is a result of the object's default toString()method which prints out the class name and the hash code. You can actually change this by overriding the toString()method in the class of the object that you want to print out. However, instead of printing the object itself, you'd have to call toString()on the object. For example:

这是对象的默认toString()方法打印出类名和哈希码的结果。您实际上可以通过覆盖toString()要打印的对象的类中的方法来更改此设置。但是,您必须调用toString()该对象,而不是打印对象本身。例如:

public class demo{

    void multiply(){
        System.out.println("HELLO WORLD!")
    }

    public String toString() {
        return "This is the class, demo.";
    }


    public static void main(String args[]){
        demo e=new demo();
        demo e1=new demo();
        System.out.println(e.toString());
        System.out.println(e1.toString());
    }
}

P.S:The "default value" of an object is nullwhen the object is not initialized. However, the default values within the object are defined by whatever is in the constructor. I suggest you look into objects and classes more.

PS:对象的“默认值”是null对象没有初始化的时候。但是,对象中的默认值由构造函数中的任何内容定义。我建议你更多地研究对象和类。

回答by Nick Holt

The term 'default value for an object' isn't really correct. Objects don't default, references do and they default to nullas described in the the documentation on Data Types.

术语“对象的默认值”并不正确。对象不是默认的,引用是默认的,并且它们默认nullData Types文档中的描述。

What you are seeing output by your program is the Stringreturned by the default implementation of toString(). When you call PrintStream.println(Object), which is what System.out.println(demo)is doing, the PrimeStreamneeds to convert the object passed, in this case your Demoobject to a String, which it does by simply calling its toString()method.

您看到的程序输出是toString()String的默认实现返回的。当你调用PrintStream.println(对象),这是在做什么,在PrimeStream需要传递的对象,你的转换在这种情况下对象的,它不会通过简单地调用它的toString()方法。System.out.println(demo)DemoString

If you haven't overridden toString()in you class then you get the default behavior.

如果您没有在类中覆盖toString(),那么您将获得默认行为。