U 'll be right, i have never used concat before. so i decided to test with
2million
the same operations at once. And the result;
"+" operand java assembly codes will be as follows cost:more than 2 minutes
public synchronized class test extends Object
{
Method public void <init>()
>> max_stack=1, max_locals=1 <<
0 aload_0
1 invokenonvirtual #1 <Method Object.<init>():void>
4 return
Line number table:
pc line
0 1
Method public static void main(String[])
>> max_stack=2, max_locals=2 <<
0 ldc #2 <String "test">
2 astore_1
3 new #3 <Class StringBuffer>
6 dup
7 invokenonvirtual #4 <Method StringBuffer.<init>():void>
10 aload_1
11 invokevirtual #5 <Method StringBuffer.append(String):StringBuffer>
14 ldc #6 <String "1">
16 invokevirtual #5 <Method StringBuffer.append(String):StringBuffer>
19 invokevirtual #7 <Method StringBuffer.toString():String>
22 astore_1
23 return
Line number table:
pc line
0 6
3 7
23 9
}
String.concat() method will be as follows cost: more than 1minutes
public synchronized class test extends Object
{
Method public void <init>()
>> max_stack=1, max_locals=1 <<
0 aload_0
1 invokenonvirtual #1 <Method Object.<init>():void>
4 return
Line number table:
pc line
0 1
Method public static void main(String[])
>> max_stack=2, max_locals=2 <<
0 ldc #2 <String "test">
2 astore_1
3 aload_1
4 ldc #3 <String "1">
6 invokevirtual #4 <Method String.concat(String):String>
9 astore_1
10 return
Line number table:
pc line
0 6
3 7
10 9
}
with stringbuffer object cost:235 millis
public synchronized class test extends Object
{
Method public void <init>()
>> max_stack=1, max_locals=1 <<
0 aload_0
1 invokenonvirtual #1 <Method Object.<init>():void>
4 return
Line number table:
pc line
0 1
Method public static void main(String[])
>> max_stack=3, max_locals=3 <<
0 new #2 <Class StringBuffer>
3 dup
4 ldc #3 <String "test">
6 invokenonvirtual #4 <Method StringBuffer.<init>(String):void>
9 astore_1
10 aload_1
11 ldc #5 <String "1">
13 invokevirtual #6 <Method StringBuffer.append(String):StringBuffer>
16 pop
17 aload_1
18 invokevirtual #7 <Method StringBuffer.toString():String>
21 astore_2
22 return
Line number table:
pc line
0 6
10 7
17 8
22 10
}