Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decompiled Java class producing different output

Out of curiosity I just decompiled below code using DJ Java Decompiler as well using CAVAJ Java Decompiler ( Java version is 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 output as: a=11 b=10 c=20



And here is 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 output as: a=11 b=11 c=20


To be more clear it has nothing to do with byte same thing happening for int as well and I even checked above codes in online compiler IDEONE and giving the same output as mine.

So, is the decompiler producing wrong code or is something else ??

like image 417
exexzian Avatar asked Sep 16 '26 11:09

exexzian


1 Answers

I will give you short answer: yes, it seems, that the decompiler is producing wrong code. Because this one:

byte b = 10;
b = b++;

has strongly predicted behavior (b will not change).

UPD: Furthermore, no one of decompilers can give you 100% warranty of the correctness of generated decompiled code.

UPD-2: Are you sure, that you provide us actual version of your code? Because this one:

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

of course is a mistake. It even will not compile :)

UPD-3 Well, I need to say, that Jad decompiler (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: Cavaj Java decompiler uses Jad as its Java decompiling engine.

Conclusion: Consider this behavior as a feature/bug of Jad decompiler, which is far from a perfection.

like image 105
Andremoniy Avatar answered Sep 19 '26 03:09

Andremoniy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!