Reputation: 1451
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?
class CharacterTest{
public static void main(String[] args){
char t=140;
char f='t';
char p='t';
System.out.print(t);
}
}
import java.io.PrintStream;
class CharacterTest
{
public static void main(String[] paramArrayOfString)
{
char c = '';
int i = 116;
int j = 116;
System.out.print(c);
}
}
Upvotes: 4
Views: 1072
Reputation: 74760
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.
Upvotes: 0
Reputation: 242696
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:
Upvotes: 1
Reputation: 39197
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.
Upvotes: 2
Reputation: 10580
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?
Upvotes: 0