<tfoot id='9k1HM'></tfoot>
  1. <i id='9k1HM'><tr id='9k1HM'><dt id='9k1HM'><q id='9k1HM'><span id='9k1HM'><b id='9k1HM'><form id='9k1HM'><ins id='9k1HM'></ins><ul id='9k1HM'></ul><sub id='9k1HM'></sub></form><legend id='9k1HM'></legend><bdo id='9k1HM'><pre id='9k1HM'><center id='9k1HM'></center></pre></bdo></b><th id='9k1HM'></th></span></q></dt></tr></i><div id='9k1HM'><tfoot id='9k1HM'></tfoot><dl id='9k1HM'><fieldset id='9k1HM'></fieldset></dl></div>
  2. <legend id='9k1HM'><style id='9k1HM'><dir id='9k1HM'><q id='9k1HM'></q></dir></style></legend>
      <bdo id='9k1HM'></bdo><ul id='9k1HM'></ul>

    <small id='9k1HM'></small><noframes id='9k1HM'>

      在 Java 中连接字符串是否总是会导致在内存中创建新字符串?

      Does concatenating strings in Java always lead to new strings being created in memory?(在 Java 中连接字符串是否总是会导致在内存中创建新字符串?)

      <small id='0VfDV'></small><noframes id='0VfDV'>

        <bdo id='0VfDV'></bdo><ul id='0VfDV'></ul>

        <legend id='0VfDV'><style id='0VfDV'><dir id='0VfDV'><q id='0VfDV'></q></dir></style></legend>

                  <tbody id='0VfDV'></tbody>

                <i id='0VfDV'><tr id='0VfDV'><dt id='0VfDV'><q id='0VfDV'><span id='0VfDV'><b id='0VfDV'><form id='0VfDV'><ins id='0VfDV'></ins><ul id='0VfDV'></ul><sub id='0VfDV'></sub></form><legend id='0VfDV'></legend><bdo id='0VfDV'><pre id='0VfDV'><center id='0VfDV'></center></pre></bdo></b><th id='0VfDV'></th></span></q></dt></tr></i><div id='0VfDV'><tfoot id='0VfDV'></tfoot><dl id='0VfDV'><fieldset id='0VfDV'></fieldset></dl></div>

                <tfoot id='0VfDV'></tfoot>
              1. 本文介绍了在 Java 中连接字符串是否总是会导致在内存中创建新字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个不适合屏幕宽度的长字符串.例如.

                I have a long string that doesn't fit the width of the screen. For eg.

                String longString = "This string is very long. It does not fit the width of the screen. So you have to scroll horizontally to read the whole string. This is very inconvenient indeed.";
                

                为了方便阅读,我想到了这样写——

                To make it easier to read, I thought of writing it this way -

                String longString = "This string is very long." + 
                                    "It does not fit the width of the screen." +
                                    "So you have to scroll horizontally" +
                                    "to read the whole string." +
                                    "This is very inconvenient indeed.";
                

                但是,我意识到第二种方法使用字符串连接,会在内存中创建 5 个新字符串,这可能会导致性能下降.是这样吗?或者编译器是否足够聪明,可以找出我真正需要的只是一个字符串?我怎么能避免这样做呢?

                However, I realized that the second way uses string concatenation and will create 5 new strings in memory and this might lead to a performance hit. Is this the case? Or would the compiler be smart enough to figure out that all I need is really a single string? How could I avoid doing this?

                推荐答案

                我意识到第二种方式使用字符串连接,会在内存中创建 5 个新字符串,这可能会导致性能下降.

                I realized that the second way uses string concatenation and will create 5 new strings in memory and this might lead to a performance hit.

                不,不会的.由于这些是字符串文字,它们将在编译时进行评估,并且只会创建一个字符串.这是在 Java 语言中定义的规范#3.10.5:

                No it won't. Since these are string literals, they will be evaluated at compile time and only one string will be created. This is defined in the Java Language Specification #3.10.5:

                一个长字符串总是可以被分解成更短的部分,并使用字符串连接运算符 +
                写成一个(可能带括号的)表达式[...]
                此外,字符串字面量总是引用 String 类的同一个实例.

                A long string literal can always be broken up into shorter pieces and written as a (possibly parenthesized) expression using the string concatenation operator +
                [...]
                Moreover, a string literal always refers to the same instance of class String.

                • 由常量表达式(第 15.28 节)计算的字符串在编译时计算,然后将其视为文字.
                • 在运行时通过串联计算的字符串是新创建的,因此是不同的.

                测试:

                public static void main(String[] args) throws Exception {
                    String longString = "This string is very long.";
                    String other = "This string" + " is " + "very long.";
                
                    System.out.println(longString == other); //prints true
                }
                

                但是,下面的情况情况不同,因为它使用了一个变量——现在有一个连接,并且创建了几个字符串:

                However, the situation situation below is different, because it uses a variable - now there is a concatenation and several strings are created:

                public static void main(String[] args) throws Exception {
                    String longString = "This string is very long.";
                    String is = " is ";
                    String other = "This string" + is + "very long.";
                
                    System.out.println(longString == other); //prints false
                }
                

                这篇关于在 Java 中连接字符串是否总是会导致在内存中创建新字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)

                    <bdo id='zE9n8'></bdo><ul id='zE9n8'></ul>
                    <tfoot id='zE9n8'></tfoot>

                    • <legend id='zE9n8'><style id='zE9n8'><dir id='zE9n8'><q id='zE9n8'></q></dir></style></legend>
                        <tbody id='zE9n8'></tbody>

                        <i id='zE9n8'><tr id='zE9n8'><dt id='zE9n8'><q id='zE9n8'><span id='zE9n8'><b id='zE9n8'><form id='zE9n8'><ins id='zE9n8'></ins><ul id='zE9n8'></ul><sub id='zE9n8'></sub></form><legend id='zE9n8'></legend><bdo id='zE9n8'><pre id='zE9n8'><center id='zE9n8'></center></pre></bdo></b><th id='zE9n8'></th></span></q></dt></tr></i><div id='zE9n8'><tfoot id='zE9n8'></tfoot><dl id='zE9n8'><fieldset id='zE9n8'></fieldset></dl></div>
                      1. <small id='zE9n8'></small><noframes id='zE9n8'>