Search code examples
javadecompiling

why there is difference between original and compiled code


Here i am tried to compile the java file and i used java decompiler to check the compiled code.why char is converted int and some of the variable names also changed?

ORIGINAL JAVA FILE

class CharacterTest{
public static void main(String[] args){

    char t=140; 
    char f='t';
    char p='t';
    System.out.print(t);

}
}

COMPILED CODE

import java.io.PrintStream;

class CharacterTest
{
public static void main(String[] paramArrayOfString)
{
char c = '';
int i = 116;
int j = 116;
System.out.print(c);
}
}

Solution

  • It completely depends on decompiler implementation details. Decompiler doesn't know names and types of your local variables, so it has to use some heuristics to reconstruct it from the bytecode.

    Your bytecode looks as follows:

       0:   sipush  140   
       3:   istore_1      
       4:   bipush  116   
       6:   istore_2      
       7:   bipush  116   
       9:   istore_3      
    

    As you can see 140 is treated as a constant of type short, whereas 116 is treated as a constant of type byte (it's caused by the fact that 116 fits into a signed byte, but 140 doesn't).

    Now decompiler tries to guess what could it mean in the source code. It looks like decompiler treats difference in constant types as difference in the types of local variables (also it can use the signature of print() choosen by compiler as a hint to determine the type of t), and variable names are generated depending on types (c for char, i and j for int).

    See also: