假设字符串a和b:

a += b
a = a.concat(b)

在引擎盖下,它们是一样的吗?

这里是concat反编译作为参考。我希望能够反编译+操作符以及看看它做什么。

public String concat(String s) {

    int i = s.length();
    if (i == 0) {
        return this;
    }
    else {
        char ac[] = new char[count + i];
        getChars(0, count, ac, 0);
        s.getChars(0, i, ac, count);
        return new String(0, count + i, ac);
    }
}

当前回答

基本上,+和concat方法之间有两个重要的区别。

If you are using the concat method then you would only be able to concatenate strings while in case of the + operator, you can also concatenate the string with any data type. For Example: String s = 10 + "Hello"; In this case, the output should be 10Hello. String s = "I"; String s1 = s.concat("am").concat("good").concat("boy"); System.out.println(s1); In the above case you have to provide two strings mandatory. The second and main difference between + and concat is that: Case 1: Suppose I concat the same strings with concat operator in this way String s="I"; String s1=s.concat("am").concat("good").concat("boy"); System.out.println(s1); In this case total number of objects created in the pool are 7 like this: I am good boy Iam Iamgood Iamgoodboy Case 2: Now I am going to concatinate the same strings via + operator String s="I"+"am"+"good"+"boy"; System.out.println(s); In the above case total number of objects created are only 5. Actually when we concatinate the strings via + operator then it maintains a StringBuffer class to perform the same task as follows:- StringBuffer sb = new StringBuffer("I"); sb.append("am"); sb.append("good"); sb.append("boy"); System.out.println(sb); In this way it will create only five objects.

伙计们,这就是+和concat方法的基本区别。 享受:)

其他回答

Niyaz是正确的,但同样值得注意的是,特殊的+运算符可以被Java编译器转换为更有效的东西。Java有一个StringBuilder类,它表示一个非线程安全的可变String。当执行一系列String连接时,Java编译器会静默地进行转换

String a = b + c + d;

into

String a = new StringBuilder(b).append(c).append(d).toString();

这对于大字符串来说效率更高。据我所知,使用concat方法时不会发生这种情况。

然而,concat方法在将空字符串连接到现有字符串时效率更高。在这种情况下,JVM不需要创建新的String对象,只需返回现有的String对象。请参阅concat文档来确认这一点。

所以如果你非常关心效率,那么你应该在连接可能为空的字符串时使用concat方法,否则使用+。然而,性能差异应该可以忽略不计,您可能永远都不应该担心这一点。

基本上,+和concat方法之间有两个重要的区别。

If you are using the concat method then you would only be able to concatenate strings while in case of the + operator, you can also concatenate the string with any data type. For Example: String s = 10 + "Hello"; In this case, the output should be 10Hello. String s = "I"; String s1 = s.concat("am").concat("good").concat("boy"); System.out.println(s1); In the above case you have to provide two strings mandatory. The second and main difference between + and concat is that: Case 1: Suppose I concat the same strings with concat operator in this way String s="I"; String s1=s.concat("am").concat("good").concat("boy"); System.out.println(s1); In this case total number of objects created in the pool are 7 like this: I am good boy Iam Iamgood Iamgoodboy Case 2: Now I am going to concatinate the same strings via + operator String s="I"+"am"+"good"+"boy"; System.out.println(s); In the above case total number of objects created are only 5. Actually when we concatinate the strings via + operator then it maintains a StringBuffer class to perform the same task as follows:- StringBuffer sb = new StringBuffer("I"); sb.append("am"); sb.append("good"); sb.append("boy"); System.out.println(sb); In this way it will create only five objects.

伙计们,这就是+和concat方法的基本区别。 享受:)

做一些简单的测试怎么样?使用下面的代码:

long start = System.currentTimeMillis();

String a = "a";

String b = "b";

for (int i = 0; i < 10000000; i++) { //ten million times
     String c = a.concat(b);
}

long end = System.currentTimeMillis();

System.out.println(end - start);

“a + b”版本在2500ms内执行。 a.concat(b)在1200ms内执行。

测试了几次。concat()版本的执行平均花费了一半的时间。

这个结果让我很惊讶,因为concat()方法总是创建一个新字符串(它返回一个“new string (result)”)。众所周知:

String a = new String("a") // more than 20 times slower than String a = "a"

为什么编译器不能在“a + b”代码中优化字符串创建,知道它总是导致相同的字符串?它可以避免创建新的字符串。 如果你不相信上面的说法,自己测试一下。

注意s.concat(“hello”);当s为空时,将导致NullPointereException。在Java中,+操作符的行为通常由左操作数决定:

System . out。printin (3 + a);/ / 100

但是,字符串是个例外。如果任意一个操作数是String,则预期结果是String。这就是null被转换为“null”的原因,即使你可能期望一个RuntimeException。

我不这么想。

a.concat(b)是在String中实现的,我认为自早期java机器以来,实现没有太大变化。+操作的实现取决于Java版本和编译器。目前+是使用StringBuffer实现的,以使操作尽可能快。也许在未来,这种情况会改变。在java +的早期版本中,对字符串的操作要慢得多,因为它产生中间结果。

我猜+=是使用+实现的,并进行了类似的优化。