Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6680327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:27:45+00:00 2026-05-26T04:27:45+00:00

I wonder if JVM/javac is smart enough to turn // This line… string a

  • 0

I wonder if JVM/javac is smart enough to turn

// This line...
string a = foo();

string foo()
{
  return bar();
}

string bar()
{
  return some-complicated-string computation;
}

into

string a = bar();

Or strip unnecessary call to foo() in release case (because unreachable code):

string a = foo(bar());

// bar is the same
...

string foo(string b)
{
  if (debug) do-something-with(b);
}

My feeling is yes for the first example and “not so sure” for the second one, but could anyone give me some pointers/links to confirm that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:27:45+00:00Added an answer on May 26, 2026 at 4:27 am

    javac will present bytecode that is a faithful representation of the original Java program that generated the bytecode (except in certain situations when it can optimize: constant folding and dead-code elimination). However, optimization may be performed by the JVM when it uses the JIT compiler.

    For the first scenario it looks like the JVM supports inlining (see under Methods here and see here for an inlining example on the JVM).

    I couldn’t find any examples of method inlining being performed by javac itself. I tried compiling a few sample programs (similar to the one you have described in your question) and none of them seemed to directly inline the method even when it was final. It would seem that these kind of optimizations are done by the JVM’s JIT compiler and not by javac. The “compiler” mentioned under Methods here seems to be the HotSpot JVM’s JIT compiler and not javac.

    From what I can see, javac supports dead-code elimination (see the example for the second case) and constant folding. In constant folding, the compiler will precalculate constant expressions and use the calculated value instead of performing the calculation during runtime. For example:

    public class ConstantFolding {
    
       private static final int a = 100;
       private static final int b = 200;
    
       public final void baz() {
          int c = a + b;
       }
    }
    

    compiles to the following bytecode:

    Compiled from "ConstantFolding.java"
    public class ConstantFolding extends java.lang.Object{
    private static final int a;
    
    private static final int b;
    
    public ConstantFolding();
      Code:
       0:   aload_0
       1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
       4:   return
    
    public final void baz();
      Code:
       0:   sipush  300
       3:   istore_1
       4:   return
    
    }
    

    Note that the bytecode has an sipush 300 instead of aload‘s getfields and an iadd. 300 is the calculated value. This is also the case for private final variables. If a and b were not static, the resulting bytecode will be:

    Compiled from "ConstantFolding.java"
    public class ConstantFolding extends java.lang.Object{
    private final int a;
    
    private final int b;
    
    public ConstantFolding();
      Code:
       0:   aload_0
       1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
       4:   aload_0
       5:   bipush  100
       7:   putfield    #2; //Field a:I
       10:  aload_0
       11:  sipush  200
       14:  putfield    #3; //Field b:I
       17:  return
    
    public final void baz();
      Code:
       0:   sipush  300
       3:   istore_1
       4:   return
    
    }
    

    Here also, an sipush 300 is used.

    For the second case (dead-code elimination), I used the following test program:

    public class InlineTest {
    
       private static final boolean debug = false;
    
       private void baz() {
          if(debug) {
             String a = foo();
          }
       }
    
       private String foo() {
          return bar();
       }
    
       private String bar() {
          return "abc";
       }
    }
    

    which gives the following bytecode:

    Compiled from "InlineTest.java"
    public class InlineTest extends java.lang.Object{
    private static final boolean debug;
    
    public InlineTest();
      Code:
       0:   aload_0
       1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
       4:   return
    
    private void baz();
      Code:
       0:   return
    
    private java.lang.String foo();
      Code:
       0:   aload_0
       1:   invokespecial   #2; //Method bar:()Ljava/lang/String;
       4:   areturn
    
    private java.lang.String bar();
      Code:
       0:   ldc #3; //String abc
       2:   areturn
    
    }
    

    As you can see, the foo is not called at all in baz because the code inside the if block is effectively “dead”.

    Sun’s (now Oracle’s) HotSpot JVM combines interpretation of the bytecode as well as JIT compilation. When bytecode is presented to the JVM the code is initially interpreted, but the JVM will monitor the bytecode and pick out parts that are frequently executed. It coverts these parts into native code so that they will run faster. For piece of bytecode that are not used so frequently, this compilation is not done. This is just as well because compilation has some overhead. So it’s really a question of tradeoff. If you decide to compile all bytecode to nativecode, then the code can have a very long start-up delay.

    In addition to monitoring the bytecode, the JVM can also perform static analysis of the bytecode as it is interpreting and loading it to perform further optimization.

    If you want to know the specific kinds of optimizations that the JVM performs, this page at Oracle is pretty helpful. It describes the performance techniques used in the HotSpot JVM.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

wonder if I could get some advice on the best way to solve this
Wonder if anyone could shed some light on this http://jsfiddle.net/orbitalmedia/ZrxBb/7/ Basically I'm trying to
I wonder if someone could help me figure out how to parse a string
I wonder what is the best way to measure the execution time of some
I wonder whether JavaFX will keep using the main JVM shipped by JavaSE and
I wonder how can I reconcile the following error? JVM cannot use large page
Apologies for the long post, but I wonder if I could get some more
Wonder if this possible. Saw many posts on adding watermark after the pdf is
i want to try CompressedOops on my JVM. No I wonder if it might
Wonder if anyone can help me with this problem. I have created a Window

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.