Decompiled Java class producing different output

Out of curiosity, I just decompiled below code using DJ Decompiler Java using CavAJ Java Decompiler (Java version - 1.7) here is the normal source code:


    byte a = 10;
    a = (byte) (a +1);

    byte b = 10;
    b = b++;

    byte c = 10;
    c +=c;

    System.out.printf("a=%d \t b=%d \t c=%d\n",a,b,c);

      

Which shows the output as: a = 11 b = 10 c = 20



And here's the decompiled one:

    byte a = 10;
    a++;
    byte b = 10;
    b++;
    b = b;
    byte c = 10;
    c += c;
    System.out.printf("a= %d \t b = %d \t c = %d\n", new Object[] {
        Byte.valueOf(a), Byte.valueOf(b), Byte.valueOf(c)
    });

      

Which, when used as source code, is output as: a = 11 b = 11 c = 20


To be more clear, this has nothing to do with byte

the same case for int

, and I even checked the above codes in the IDEONE online compiler and gives the same result as mine.

So, the decompiler generates the wrong code or something else?

+3


source to share


2 answers


I'll give you a short answer: yes, it seems that the decompiler is generating the wrong code. Because this one:

byte b = 10;
b = b++;

      

strongly predicted behavior ( b

won't change).

UPD: Also, none of the decompilers can provide you with a 100% guarantee that the generated decompiled code is correct.

UPD-2: Are you sure you provided us with the actual version of your code? Because this one:

byte aa = 10;
a = (byte) (a +1);

      

of course a mistake. It won't even compile :)



UPD-3 Well, I have to say that the decompiler Jad

(Jad 1.5.8g for Windows 9x / NT / 2000 on Intel platform) produces the same code:

    byte b = 10;
    b++;
    b = b;

      

...

    java.lang.System.out.printf("a=%d \t b=%d \t c=%d\n", new java.lang.Object[] {...

      

But this is not surprising: the Cavaj Java decompiler uses Jad as its Java decompiler .

Conclusion: Consider this behavior as a function / error of a decompiler Jad

which is far from perfect.

+4


source


It is not a decompiler. When you compile your code with the java compiler, in some cases the compiler will change your code and optimize it for other code that doesn't change the result. Your code:

byte a = 10;
a = (byte) (a + 1);

      

In this case, the java compiler knows that there is no need to type a letter between byte and byte, and because of this, the compiler tries to remove your type and in the process it replaces your code with the closest code it can understand.This is the compiler output:



byte a = 10;
a++;

      

So, this is not a decompilation process. The java compiler modifies your code.

-2


source







All Articles