简体   繁体   English

为什么打印两次“ tostring”?

[英]why does it print “tostring” twice?

here is my class please explain why does it prints "tostring" twice 这是我的课,请解释为什么它两次打印“ tostring”

public class HashCodeAndEquals 
{
    static String asd;

    public HashCodeAndEquals(String string) {
        asd = string;
    }


    /**
     * @param args
     */
    public static void main(String[] args) {
        HashCodeAndEquals obj1 = new HashCodeAndEquals("one");
        HashCodeAndEquals obj2 = new HashCodeAndEquals("two");
        System.out.println(obj1.toString());
        //System.out.println(obj2.toString());
        System.out.println(obj1.equals(obj2));
        System.out.println(obj1==obj2);

    }


    @Override
    public String toString() {
        // TODO Auto-generated method stub
        System.out.println("tostring");
        return "tostring";
    }

    @Override
    public int hashCode() {
        // TODO Auto-generated method stub
        System.out.println("hashcode");
        return 0;
    }

}

why does it calls toString method twice ?? 为什么它两次调用toString方法? at what point hash code is called and does hashcode() calls tostring() or is called by toString() ??? 在什么时候调用哈希码,并且hashcode()调用tostring()或被toString()调用?

Don't have a System.out.println(...) call from within the toString() method as it makes no sense. 不要在toString()方法中进行System.out.println(...)调用,因为这没有任何意义。 The goal of this method is not to print a String but rather to return a String, one that the calling code can then decide what to do with, including printing it out, or displaying it in a GUI, or whatever. 此方法的目标不是打印字符串,而是返回字符串,调用者可以随后决定该字符串做什么,包括打印出来或在GUI中显示它,等等。

so change this: 所以改变这个:

public String toString() {
    System.out.println("tostring");
    return "tostring";
}

to this: 对此:

public String toString() {
    return "tostring";
}

As an aside, the System.out.println method automatically calls toString() on objects it's printing, and so there's no need to explicitly call this from within the method parameter. 顺便说一句,System.out.println方法会在要打印的对象上自动调用toString() ,因此无需从method参数内部显式调用此方法。

Thus you can change this: 因此,您可以更改此设置:

System.out.println(obj1.toString());

to the more concise: 更简洁:

System.out.println(obj1);

As another aside, you are planning on changing your hashCode() method, right? 另外,您打算更改您的hashCode()方法,对吗?


Regarding, 关于,

at what point hash code is called and does hashcode() calls tostring() or is called by toString() ? 在什么时候调用哈希码,并且hashcode()调用tostring()还是被toString()调用?

Your program should tell you exactly when hashCode() is called as your override has a println within it. 您的程序应准确告诉您何时调用hashCode() ,因为您的重写中包含println。 It will be called if you place your object in a HashSet or use it as a key to a HashMap. 如果将对象放置在HashSet中或将其用作HashMap的键,则将调用该方法。 It is also used when checking for equality in collections (I believe), but test your code to see when and where it is used. 检查集合中的相等性时也使用它(我相信),但是请测试您的代码以查看何时和何处使用它。 Also make hashCode more useful by having it return non-0 values that depend on states of the key fields of your class, the same fields used in your equals method test. 还可以通过使hashCode返回非0值来使hashCode更加有用,该值取决于类的键字段的状态,即equals方法测试中使用的相同字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM