java 字符串缓冲区声明

发布于 2024-08-22 22:09:36 字数 429 浏览 9 评论 0原文

当我不包含注释行时,prevLineBuffer 包含“null”。当我包含注释行时,它仍然有效,但打印一个空字符串。 Java 是否为声明的字符串静态分配空间,然后为注释行中的附加字符串动态分配空间?两者似乎都有效...

public class Indexer {
    String input;
    StringBuilder prevLineBuffer;

    Indexer(String inputFileName) throws RuntimeException{
        input = inputFileName;
        //prevLineBuffer = new StringBuilder();
        System.out.print(prevLineBuffer);

    }//constructor
}//class

When I don't include the commented line, prevLineBuffer contains "null." When i do include the commented line it still works, but prints an empty string. Does Java statically allocate space for the declared string and then dynamically allocate space for an additional string in the commented line? Both appear to work...

public class Indexer {
    String input;
    StringBuilder prevLineBuffer;

    Indexer(String inputFileName) throws RuntimeException{
        input = inputFileName;
        //prevLineBuffer = new StringBuilder();
        System.out.print(prevLineBuffer);

    }//constructor
}//class

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

计㈡愣 2024-08-29 22:09:36

您需要打印 .toString() 的结果,并为其添加 .append() 内容,以便为其提供打印内容。例如:

    prevLineBuffer = new StringBuilder();
    prevLineBuffer.append(inputFileName);
    System.out.print(prevLineBuffer.toString());

要回答问题的后半部分...

来自 字符串文档

字符串池最初为空,由 String 类私有维护。 [...] 调用 intern 方法时,如果池中已经包含由 equals(Object) 方法确定的等于此 String 对象的字符串,则返回池中的字符串。否则,将此 String 对象添加到池中,并返回对此 String 对象的引用。

来自 StringBuilder 文档

一般来说,如果 sb 引用 StringBuilder 的实例,则 sb.append(x) 与 sb.insert(sb.length(), x) 具有相同的效果。每个字符串生成器都有一个能力。只要字符串构建器中包含的字符序列的长度不超过容量,就不需要分配新的内部缓冲区。如果内部缓冲区溢出,它会自动变大。 -

You need to print the result of .toString(), and .append() something to it, in order to give it something to print. For example:

    prevLineBuffer = new StringBuilder();
    prevLineBuffer.append(inputFileName);
    System.out.print(prevLineBuffer.toString());

To answer the second half of your question...

From the String docs:

A pool of strings, initially empty, is maintained privately by the class String. [...] When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.

From the StringBuilder docs:

In general, if sb refers to an instance of a StringBuilder, then sb.append(x) has the same effect as sb.insert(sb.length(), x). Every string builder has a capacity. As long as the length of the character sequence contained in the string builder does not exceed the capacity, it is not necessary to allocate a new internal buffer. If the internal buffer overflows, it is automatically made larger. -

桃气十足 2024-08-29 22:09:36

简而言之,将任何内容的空引用打印到 PrintStream(这就是 System.out)都会附加“null”。这是因为 print() 和 println() 方法显式测试 null,使用类似以下内容: txt=(obj==null ? "null" : obj.toString());

当您创建StringBuilder 而不是将其保留为 null,打印的是 StringBuilder 的 toString(),如果您没有添加任何内容,则它是一个空字符串,或者...什么都没有。

In short, printing a null reference of anything to a PrintStream (which is what System.out is) will append "null". This is because the print() and println() methods explicitly test for null, using something like: txt=(obj==null ? "null" : obj.toString());

When you create the StringBuilder instead of leaving it null, what is printed is StringBuilder's toString(), which if you have added nothing is an empty string, or... nothing.

情场扛把子 2024-08-29 22:09:36

Java 不为对象静态分配空间。该行

StringBuilder prevLineBuffer;

声明了对 StringBuilder 的引用。在调用构造函数之前,StringBuilder 本身并不存在。因此,您会得到一个 NullPointerException - prevLineBuffer 引用为 null。

您的 prevLineBuffer 为空的原因很简单,就是您从未向其中添加任何内容 - 另请参阅其他回复。

Java does not statically allocate space for objects. The line

StringBuilder prevLineBuffer;

declares a reference to a StringBuilder. The StringBuilder itself does not exist before you invoke the constructor. Thus you get a NullPointerException - the prevLineBuffer reference is null.

The reason why your prevLineBuffer is empty is simply that you never append any content to it - see also the other replies.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文