1

Are there any optimizations, such as dead code elimination, involved when translating java source files to bytecodes?

3

2 Answers 2

2

The standard Java compilers do few optimizations on the emitted bytecodes. I think that the reasoning is that unoptimized bytecodes will be easier for the HotSpot JIT compiler to optimize.

The links that @Mitch Wheat provided in comments above (particularly the 2nd one) date from the days when HotSpot JIT was new technology.

Sign up to request clarification or add additional context in comments.

Comments

0

While searching all source code optimization, i came across this question and though I am answering after long time this question asked.

After jdk1.7, String concatenation using plus [+] operator converted to StringBuilder append e.g

public static void main(String[] args) {
String s = new String("");
s = s+"new";
}

Converted to StringBuilder append as shown in bytecode

public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=3, locals=2, args_size=1
         0: new           #2                  // class java/lang/String
         3: dup
         4: ldc           #3                  // String
         6: invokespecial #4                  // Method java/lang/String."<init>":(Ljava/lang/String;)V
         9: astore_1
        10: new           #5                  // class java/lang/StringBuilder
        13: dup
        14: invokespecial #6                  // Method java/lang/StringBuilder."<init>":()V
        17: aload_1
        18: invokevirtual #7                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
        21: ldc           #8                  // String new
        23: invokevirtual #7                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
        26: invokevirtual #9                  // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
        29: astore_1
        30: return
      LineNumberTable:
        line 13: 0
        line 14: 10
        line 15: 30
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      31     0  args   [Ljava/lang/String;
           10      21     1     s   Ljava/lang/String;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.