#Compiled Code Questions

13 messages · Page 1 of 1 (latest)

median timber
#

I probably won't understand any answers since this is probably too complex for me right now.

Was learning about polymorphism today and thought the compiled code might show methods used to make my life easier in this scenario.

    public static void main(String[] args) {
        System.out.println(4 + 0.5);
        
        int a = 4;
        double b = 0.5;
        System.out.println(a + b);
    }
}```

The compiled code below:

```// Source code is decompiled from a .class file using FernFlower decompiler.
public class Main {
   public Main() {
   }

   public static void main(String[] var0) {
      System.out.println(4.5);
   }
}``` 

**Now I've got a few thoughts:**
> Is the compiler essentially optimizing my code?
> 
> There are two print statements in the original code but only one in the compiled code. How is it able to print twice? 
> 
> If the compiler is optimizing my code, would it be good practice to regularly check my compiled code to see what changes it makes?
deft raftBOT
#

This post has been reserved for your question.

Hey @median timber! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

median timber
#

Also to throw another question out there, why does "args" in the main method become "var0" in the compiled code?

pure pendant
#

First of all, what you have shown isn't the compiled code but the decompiled code

#

Decompiling means getting back something similar to the code you have

#

Secondly, there are multiple compilers involved. javac doesn't optimize that much, most of the optimizations are done by the JIT compiler at runtime.

#

One of the optimizations javac does is inlining literals and constants which you can see from the addition a + b being transformed to 4.5

#

No, there's pretty much no reason to check the compiled code

pure pendant
#

Thr information is no longer there so the decompiler needs to insert something and uses var0

median timber
#

Thanks for this info. I feel quite dumb now although glad that I won't be going around believing that was compiled code.

deft raftBOT