Why is there a difference between source and compiled code

Here I am trying to compile a java file, and I used the java decompiler to check the compiled code .why char is converted to int, and some of the variable names are 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); } } 

Summary 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); } } 
+4
source share
4 answers

It completely depends on the implementation details of the decompiler. Decompiler does not know the names and types of your local variables, so it must use some heuristics to recover it from bytecode.

Your bytecode is 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 treated as a constant of type short , while 116 treated as a constant of type byte (this is because 116 fits into the signed byte, but 140 not).

Now the decompiler is trying to guess what this might mean in the source code. It seems that the decompiler considers the difference in constant types as the difference in the types of local variables (it can also use the print() signature chosen by the compiler as a hint for determining the type of t ), and variable names are generated depending on the types ( c for char , i and j for int ).

See also:

+1
source

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

And the second name of the local variable is not contained in the class file. Thus, the decompiler must invent its own names.

+2
source

A char is indeed an integer type.

The char type (character) contains one 16-bit Unicode character, which is actually represented by unsigned 16-bit integers.

(source: SCJP from Kathy Sierra)

I don’t know about the naming change. But I think this is the problem of your decompiler. Have you tried different decompilers and saw variable names that all produced?

0
source

As said, local variables for a virtual machine can have only types (or, more precisely, can be accessed with commands for) int , long , float , double and reference - therefore, all integer types (except long ones) are considered as int inside.

The first variable remains char , since it 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 no longer used, so they remain int here.

0
source

Source: https://habr.com/ru/post/1344448/


All Articles