for loop getting converted to do while loop in compiled class file

久未见 提交于 2020-02-05 02:32:27

问题


In my team we are using java 1.4.2 Most of the machine for loop is getting compiled as for only.ie. if i decompile the class file I can get see for loop only but certain machines of certain developer's it becomes do while loop. ie when i decompile certain classes it becomes do while

How can it happen? Any possible reason, java version or configuration any body can think so i can reproduce this defect and fix it in all developers machines


回答1:


I would not call this a defect. When you compile Java to bytecodes, some information is lost. When you subsequently decompile the bytecodes, there is no guarantee that the resulting Java source will closely match what you've started with.

In particular, the bytecode language has no specific instructions for different types of loop. Java loops get compiled into bytecodes that use comparison instructions and jumps. The decompiler has to make an educated guess when deciding which type of loop was used to produce the given bytecodes.

The difference in behaviour across machines probably has to do with differences in the exact versions of the compiler and the decompiler installed on those machines, or perhaps with how those tools are configured.




回答2:


The code for while and for are interchangable and there is no way to tell from the byte code which one was used (you could infer it) You can't reproduce comments from byte code and you cannot reliably tell the different between a for and while loop.

e.g.

while(condition)

and

for(;condition;)

and

while(true) {
   if(!condition) break;

}

are the same.




回答3:


In the bytecode, there are no loops, there are only conditional and unconditional jumps (aka gotos). So the decompiler does its best to reconstruct what loop that was based on jumps' structure.




回答4:


Compiler optimizations will never change what the code actually does.

Even if different compilers are making different optimizations to your code, there will never be any difference in the program's semantics: the user will always get the same results.

This is not a bug in need of a fix. There is no fix. There is no bug.




回答5:


it's probably caused by the compiler's optimization settings. check optimization settings on different machines. try to disable all optimizations or equal the optimization level.



来源:https://stackoverflow.com/questions/9416583/for-loop-getting-converted-to-do-while-loop-in-compiled-class-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!