开发者

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(S开发者_Go百科tring[] paramArrayOfString)
{
char c = '';
int i = 116;
int j = 116;
System.out.print(c);
}
}


JVM and bytecode do not distinguish between char and int per se. This is only on the semantic/language level.

And second local variable names are not contained in the class file. Thus the decompiler has to invent his own names.


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:

  • The Java Virtual Machine Instruction Set


A char is really an Integer type.

The char type (a character) contains a single, 16-bit Unicode character, that are actually represented by unsigned 16-bit integers.

(source: SCJP by Kathy Sierra)

About the naming change, I don't know for sure. But I guess it's an issue of your decompiler. Have you tried different decompilers and saw the variable names that everyone produced?


As said, local variables for the VM can only have the types (or more exactly, can be accessed with commands for) int, long, float, double and reference - so all integer types (apart from long) are treated as int internally.

The first variable remains char since this is the argument type of the println() method called here, so the compiler has some way to guess it, while the other two variables are not used anymore, so they stay int here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜