3

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 ??

7
  • 1
    I don't get how the additional spaces disappeared from output in the second case. Commented Jan 18, 2013 at 18:52
  • @dystroy that might be the feature of Decompiler`s editor to format the code Commented Jan 18, 2013 at 18:54
  • 2
    This should probably be discussed in CAVAJ's forum as it looks as a decompilation bug. Commented Jan 18, 2013 at 18:55
  • @Anony-Mousse I suppose he's concerned by the fact that the decompiler changed b=b++; in b++;b=b;. Commented Jan 18, 2013 at 18:57
  • Yep, looks like a CAVAJ bug. Nothing we can help with here. Commented Jan 18, 2013 at 18:57

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Wait 10 minutes, I'll check it by my self :)
@dystroy No they are of same class. BTW what made you think that
@sansix please view my UPD-2, there is some mistake in your code.
@dystroy is also info for you: I checked this case, it is really Jad's defect.
On the other hand, they'll probably explain that their decompiler helped OP find a bug in his code...
|
-2

This is not about decompiler. When you compile a code using java compiler, in some cases, the compiler changes your code and optimize it to another code that does not change the result. Your code is:

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

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

byte a = 10;
a++;

So, this is not about decompile process. The java compiler changes your code.

6 Comments

so you meant to say a = (byte) (a + 1); here type cast is not required?
Dear, we are talking about this one: b = b++;
The answer is same. b = b++; changes during compilation process not decompile. This code will replace with b++; and yes, there is no need to a typecast.
When you write a code like byte a = 0; a = (byte) (a + 1); the compiler knows that you are trying to do a++; and it will change your code and optimize it to a++; in your version, typecast required but in compiled version it is not required and the result is same. Then simply compiler changes your code to optimized version.
@SasanYavari Replacing b = b++; with b++; b = b; isn't an optimization.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.