java 返回或打印 StringBuilder?爪哇

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

Return or Print StringBuilder? Java

java

提问by whales

I am a bit confused about the StringBuilder. It seems that when I print a StringBuilder, there it no need to add .toString() because it will automatically give me a string representation. However, when I return a StringBuilder object, I have to add the .toString(). Is that true? and why?

我对 StringBuilder 有点困惑。似乎当我打印一个 StringBuilder 时,它不需要添加 .toString() 因为它会自动给我一个字符串表示。但是,当我返回一个 StringBuilder 对象时,我必须添加 .toString()。真的吗?为什么?

Also, I am bit confused about the following code:

另外,我对以下代码有些困惑:

package com.tutorialspoint;
import java.lang.*;
public class StringBuilderDemo {
    public static void main(String[] args) {

        StringBuilder str = new StringBuilder("India ");
        System.out.println("string = " + str);

        // append character to the StringBuilder
        str.append('!');
        // convert to string object and print it
        System.out.println("After append = " + str.toString());

        str = new StringBuilder("Hi "); 
        System.out.println("string = " + str);
        // append integer to the StringBuilder
        str.append(123);
        // convert to string object and print it
        System.out.println("After append = " + str.toString());
    }
}

For the different println, sometimes this code use toString and some other times it didn't. Why? I tried deleting the toString and the results are the same. Is it still necessary to use toString in println?

对于不同的 println,此代码有时使用 toString,有时不使用。为什么?我尝试删除 toString 并且结果相同。是否还需要在 println 中使用 toString?

Thanks so much for helping a newbie out!

非常感谢您帮助新手!

回答by Mena

When you print an object to a print stream, the Stringrepresentation of that object will be printed, hence toStringis invoked.

当您将对象打印到打印流时,该String对象的表示将被打印,因此toString被调用。

Some classes override Object#toString, amongst which StringBuilderdoes.

某些类会覆盖Object#toString,其中包括StringBuilder

Hence explicitly invoking toStringfor StringBuilderis unnecessary.

因此,显式调用toStringforStringBuilder是不必要的。

On the other hand, other objects don't override toString. For instance, arrays.

另一方面,其他对象不会覆盖toString. 例如,数组。

When you print an array, unless using a utility such as Arrays.toString, you're getting the array's class type @its hash code, as opposed to a human-readable representation of its contents.

当您打印数组时,除非使用诸如Arrays.toString之类的实用程序,否则您将获得数组的类类型@其哈希码,而不是其内容的人类可读表示。

回答by dguay

From the documentation:

从文档:

Note that println() prints a string builder, as in:

请注意, println() 打印一个字符串生成器,如下所示:

System.out.println(sb);

because sb.toString() is called implicitly, as it is with any other object in a println() invocation.

因为 sb.toString() 是隐式调用的,就像 println() 调用中的任何其他对象一样。

回答by libik

If you try to append an object to a string like this : "string = " + str, the toString() method is implicitly called.

如果您尝试将对象附加到这样的字符串 : "string = " + str,则隐式调用 toString() 方法。

So no, it does not matter, if you specify it.

所以不,没关系,如果你指定它。

Also the toString() method itself (even when you are not append it to string) calls the toString() method.

此外 toString() 方法本身(即使您没有将其附加到字符串)调用 toString() 方法。

Therefore System.out.println(str)and System.out.println(str.toString())gives same result.

因此System.out.println(str)System.out.println(str.toString())给出相同的结果。

回答by nick wouters

The first thing you should know about Java is that we work with objects and that all objects inherit methods from the class Object: http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html

关于 Java,您应该了解的第一件事是我们使用对象,并且所有对象都从 Object 类继承方法:http: //docs.oracle.com/javase/7/docs/api/java/lang/Object.html

This class has a toString() method and since every object inherits this method it can always be called on every object. When you do not override it, it usually returns the physical address of the object.

这个类有一个 toString() 方法,因为每个对象都继承了这个方法,所以它总是可以在每个对象上调用。当你不覆盖它时,它通常返回对象的物理地址。

Like stated in other answers, whenever a string is expected in println for instance and you pass an object it automatically calls the method which requires an Object (note the capital, we are talking about the class Object here), it will then use the toString method on the object passed along as parameter. The reason you get the string you want is because StringBuilder overrides the toString() method for you.

就像其他答案中所述,例如,只要在 println 中需要一个字符串并且您传递一个对象,它就会自动调用需要一个 Object 的方法(注意大写,我们在这里讨论的是 Object 类),然后它将使用 toString作为参数传递的对象上的方法。你得到你想要的字符串的原因是因为 StringBuilder 为你覆盖了 toString() 方法。

When you in your own code want to pass the string in your StringBuilder you have two options. You can either pass StringBuilder.toString() or change the return type to Object or StringBuilder and call toString() when you actually need it.

当您在自己的代码中想要在 StringBuilder 中传递字符串时,您有两个选择。您可以传递 StringBuilder.toString() 或将返回类型更改为 Object 或 StringBuilder 并在实际需要时调用 toString() 。

Hope this clarifies why you can just pass the object instead of the string.

希望这能澄清为什么您可以只传递对象而不是字符串。