有多少String对象将使用加号时产生的呢? [英] How many String objects will be created when using a plus sign?

查看:139
本文介绍了有多少String对象将使用加号时产生的呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

多少String对象将在下文code。使用一个加号时创造出来的?

How many String objects will be created when using a plus sign in the below code?

String result = "1" + "2" + "3" + "4";

如果是下面,我会所述的三个String对象:1,2,12

If it was as below, I would have said three String objects: "1", "2", "12".

String result = "1" + "2";

我也知道,String对象在字符串实习生池/表性能改进缓存,但是这不是问题。

I also know that String objects are cached in the String Intern Pool/Table for performance improvement, but that's not the question.

推荐答案

令人惊讶,这取决于

如果你的方法做到这一点:

If you do this in a method:

void Foo() {
    String one = "1";
    String two = "2";
    String result = one + two + "34";
    Console.Out.WriteLine(result);
}

那么编译器使用似乎发出了code String.Concat 作为@Joachim回答(+1他BTW)。

then the compiler seems to emit the code using String.Concat as @Joachim answered (+1 to him btw).

如果您将其定义为常量,例如:

If you define them as constants, e.g.:

const String one = "1";
const String two = "2";
const String result = one + two + "34";

文本,如在原来的问题:

String result = "1" + "2" + "3" + "4";

那么编译器将优化掉那些 + 的迹象。它等同于:

const String result = "1234";

此外,编译器会删除多余的不断前pressions,只放出他们,如果他们使用或暴露。举例来说,这个程序:

Furthermore, the compiler will remove extraneous constant expressions, and only emit them if they are used or exposed. For instance, this program:

const String one = "1";
const String two = "1";
const String result = one + two + "34";

public static void main(string[] args) {
    Console.Out.WriteLine(result);
}

只产生一个与字符串常量结果(等于1234)。 有一个两个不产生的IL露面。

Only generates one string- the constant result (equal to "1234"). one and two do not show up in the resulting IL.

请有可能是在运行时进一步优化。我只是通过何种IL生产下去。

Keep in mind that there may be further optimizations at runtime. I'm just going by what IL is produced.

最后,关于实习,常数和文字被扣留,但其实习的值是在IL所得恒定值,而不是字面。这意味着比你期望你可能会得到更少的字符串对象,因为多个相同定义的常量或文字实际上是同一个对象!这是通过以下示出:

Finally, as regards interning, constants and literals are interned, but the value which is interned is the resulting constant value in the IL, not the literal. This means that you might get even fewer string objects than you expect, since multiple identically-defined constants or literals will actually be the same object! This is illustrated by the following:

public class Program
{
    private const String one = "1";
    private const String two = "2";
    private const String RESULT = one + two + "34";

    static String MakeIt()
    {
        return "1" + "2" + "3" + "4";
    }   

    static void Main(string[] args)
    {
        string result = "1" + "2" + "34";

        // Prints "True"
        Console.Out.WriteLine(Object.ReferenceEquals(result, MakeIt()));

        // Prints "True" also
        Console.Out.WriteLine(Object.ReferenceEquals(result, RESULT));
        Console.ReadKey();
    }
}

在哪里字符串在一个循环连接起来(或者动态)的情况下,你最终每级联一个额外的字符串。例如,下面的代码创建12字符串实例:2常数+ 10次迭代,每次产生一个新的String实例:

In the case where Strings are concatenated in a loop (or otherwise dynamically), you end up with one extra string per concatenation. For instance, the following creates 12 string instances: 2 constants + 10 iterations, each resulting in a new String instance:

public class Program
{
    static void Main(string[] args)
    {
        string result = "";
        for (int i = 0; i < 10; i++)
            result += "a";
        Console.ReadKey();
    }
}

但是(也出奇),多个连续的级联被编译器为单个多串级联组合。例如,该计划还只生产12字符串实例!这是因为即使你使用多个运营商+在一个声明中,该字符串内容被复制一次。

public class Program
{
    static void Main(string[] args)
    {
        string result = "";
        for (int i = 0; i < 10; i++)
            result += "a" + result;
        Console.ReadKey();
    }
}

这篇关于有多少String对象将使用加号时产生的呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆