1

I want to see the bytecode of this code using javap -c.

if (3 < 5) {
        
}

But for some reason it doesn't show the steps. Unlike with int a; which works fine.

Any ideas how to see this in bytecode?

2
  • The block has nothing in it, so the compiler is probably just removing that piece of code. Try making something actually happen inside the block (something predictable, so you can remove it from the bytecode). Commented Jun 9, 2013 at 1:30
  • Not only that, the condition is always true, you're not likely to see the test either (at least with Oracle's Java 7 compiler the test is not turned in to bytecode). Commented Jun 9, 2013 at 1:34

3 Answers 3

3

The Compiler is not as dumb as you think. The block you try to see in byte code is:

  1. empty
  2. Always true

That means that the compiler will leave it out of the byte code completely.

Try the following:

    int a = 3;
    if(a < 5)
    {
        a = 3 + 5;
    }

and you will get the following assembly instructions in your byte code:

   0: iconst_3      
   1: istore_1      
   2: iload_1       
   3: iconst_5      
   4: if_icmpge     10
   7: bipush        8
   9: istore_1      
  10: return 
Sign up to request clarification or add additional context in comments.

Comments

0

In case you were wondering, the bytecode that would be generated if it wasn't optimized out entirely would be something like

iconst_3
iconst_5
if_icmpge (offset)

Comments

-1

javap -c : Print the Java Virtual Machine instructions for each of the methods in each of the specified classes. This option disassembles all methods, including private methods.

As there is nothing in your block and as the condition is always true, you may not see the byte code.

1 Comment

Your quote has nothing to do with the question.

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.