java中的toString是什么

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

What is toString in java

java

提问by Mohammad Hemel

What is the function of  toString in here?
what is the need for toString

can anyone please explain what toString suppose to do here. I am new in java and learning alot of new stuff

任何人都可以解释一下 toString 假设在这里做什么。我是 Java 新手,学习了很多新东西

public class Employee  
    {
       private String name;
       private String address;
       private int number;
       public Employee(String name, String address, int number)
       {
          System.out.println("Constructing an Employee");
          this.name = name;
          this.address = address;
          this.number = number;
       }




     public String toString() //what is  this function doing
       {
          return name + " " + address + " " + number;
       }

Heading

标题

回答by Batty

Method of Object class, which returns value of object.

Object类的方法,返回对象的值。

According to Java Docs :

根据 Java 文档:

public String toString()

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 符号字符“@”和对象哈希码的无符号十六进制表示组成。换句话说,此方法返回一个等于以下值的字符串:

Returns: a string representation of the object.

返回: 对象的字符串表示形式。

refer java docs here

在此处参考 java 文档

回答by upog

from java doc

来自 java 文档

/**
 * Returns a string representation of the object. In general, the
 * {@code 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.
 * <p>
 * The {@code toString} method for class {@code Object}
 * returns a string consisting of the name of the class of which the
 * object is an instance, the at-sign character `{@code @}', 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:
 * <blockquote>
 * <pre>
 * getClass().getName() + '@' + Integer.toHexString(hashCode())
 * </pre></blockquote>
 *
 * @return  a string representation of the object.
 */

http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#toString%28%29

http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#toString%28%29

回答by nhgrif

Consider:

考虑:

Employee coolDude = new Employee("Billy Bob McCool", "123 Main Str", "867-5309");
System.out.println(coolDude);

Without the toStringmethod you're asking about, this will print the class name and a hex number that will look like garbage to you, but it's actually the memory address to where coolDudeexists in memory. With the toStringmethod, you can actually print something useful. In this specific case, "Billy Bob McCool 123 Main Str 867-5309".

如果没有toString您询问的方法,这将打印类名和一个十六进制数字,这对您来说看起来像是垃圾,但它实际上是内存中coolDude存在的内存地址。使用该toString方法,您实际上可以打印一些有用的东西。在这种特定情况下,"Billy Bob McCool 123 Main Str 867-5309".