Java compiler not optimizing string concatenation -
i have noticed java compiler not converting string addition (+) stringbuilder.append() method. have created class has 1 method
public void dosomething(string a, string b) { string c = + "a"; string d = b + "b"; string e = c + d; string f = e; system.out.println(f); }
after compilation , decompilation method looked this:
public void dosomething(string paramstring1, string paramstring2) { string str1 = paramstring1 + "a"; string str2 = paramstring2 + "b"; string str3 = str1 + str2; string str4 = str3; system.out.println(str4); }
why compiler not optimizing code? using ant packaging , debug setting false. i've tried javac single java file result same.
i did javap -c test.class
, stringbuilder appeared (java 8).
public void dosomething(java.lang.string, java.lang.string); code: 0: new #2 // class stringbuilder 3: dup 4: invokespecial #3 // method stringbuilder."<init>":()v 7: aload_1 8: invokevirtual #4 // method stringbuilder.append:(lstring;)lstringbuilder; 11: ldc #5 // string 13: invokevirtual #4 // method stringbuilder.append:(lstring;)lstringbuilder; 16: invokevirtual #6 // method stringbuilder.tostring:()lstring; 19: astore_3 20: new #2 // class stringbuilder 23: dup 24: invokespecial #3 // method stringbuilder."<init>":()v 27: aload_2 28: invokevirtual #4 // method stringbuilder.append:(lstring;)lstringbuilder; 31: ldc #7 // string b 33: invokevirtual #4 // method stringbuilder.append:(lstring;)lstringbuilder; 36: invokevirtual #6 // method stringbuilder.tostring:()lstring; 39: astore 4 41: new #2 // class stringbuilder 44: dup 45: invokespecial #3 // method stringbuilder."<init>":()v 48: aload_3 49: invokevirtual #4 // method stringbuilder.append:(lstring;)lstringbuilder; 52: aload 4 54: invokevirtual #4 // method stringbuilder.append:(lstring;)lstringbuilder; 57: invokevirtual #6 // method stringbuilder.tostring:()lstring; 60: astore 5 62: aload 5 64: astore 6 66: getstatic #8 // field system.out:ljava/io/printstream; 69: aload 6 71: invokevirtual #9 // method java/io/printstream.println:(lstring;)v 74: return
i think decompiler tries simplify achieve natural coding.
Comments
Post a Comment