简体   繁体   English

Java逐步编译/执行/调试

[英]java step by step compile / execute / debug

I have a small piece of code where I would like to know how the debugger thinks. 我有一小段代码,我想知道调试器的想法。

/**L05*/ public class Main {
/**L06*/ public static void sp(String a){
/**L07*/    System.out.println(a);
/**L08*/ }
/**L09*/
/**L10*/ public static void main(String[] args) {
/**L11*/    sp("start");
/**L12*/
/**L13*/    //object-1
/**L14*/    Player p1 = new Player();
/**L15*/    sp("p1");
/**L16*/
/**L17*/    //object-2
/**L18*/    Player
/**L19*/            p2
/**L20*/            =
/**L21*/            new
/**L22*/                    Player
/**L23*/                    (
/**L24*/                    )
/**L25*/            ;
/**L26*/    sp
/**L27*/            (
/**L28*/                    "p2"
/**L29*/            )
/**L30*/    ;
/**L31*/
/**L32*/    //object-3
/**L33*/    Player p3= new Player(){/**strange that this works*/};
/**L34*/    Player p4=
/**L35*/    new Player();
/**L36*/    Player p5;
/**L37*/  }
/**L38*/ } 

code1 代码1

I would like to know the execution process from Line 18 till 30. 我想知道第18至30行的执行过程。

For example when I debug the code (starting now from Line 11) it goes step by step so: 例如,当我调试代码(从第11行开始)时,它会逐步进行,因此:

Line11 There it executes the method and after that it goes to Line11在那里执行方法,然后转到

Line13 because it is an comment. Line13,因为它是注释。 It do not execute it and then goes to 它不执行它,然后转到

Line 14 there it makes an object of the Player -> reserves space how much Player needs. 第14行是播放器的对象->保留播放器需要多少空间。 Calls the default-Constructor gives and reference (pointer named as p1) the type of Player and puts the Playerobject into the Player. 调用default-Constructor给出和引用(称为p1的指针)类型的Player,并将Player对象放入Player。 Endofline now it goes to 现在是Endofline

Line 15 it executes. 它执行第15行 Now it goes to 现在去

Line 17 its a comment and then it goes to 第17行是其注释,然后转到

Line 18 "?" 第18行 “?” now i get confused. 现在我很困惑。 When i debug the code -> it goes after Line 17 to Line 19 and not to Line 18 or to Line 21 or how i long time thought the compiler starts from the right side from Semicolon and goes from right to left. 当我调试代码->它从第17行到第19行,而不是第18行或第21行,或者我长期以来认为编译器从分号的右边开始,从右到左。

After Line19 it skips the other lines and goes to Line19之后,它跳过其他行并转到

Line 26 strange -> where are the lines 18,22-25 when do it execute this lines. 第26行奇怪->执行此行时,第18,22-25行在哪里。 And how/when would the debugger recognizes when i have forgot an semicolon on Line25 ? 当我在Line25上忘记分号时,调试器将如何识别/何时识别?

I have debugged my code step by step with breakpoints. 我已经使用断点逐步调试了我的代码。 The output is ok and fine. 输出正常。 The code Player.java file is not important for this question. 代码Player.java文件对于此问题并不重要。 If there is something missing or not understandable please tell me and I can add or change it. 如果缺少或无法理解,请告诉我,我可以添加或更改。

The debugger doesn't see those lines per se because nothing interesting happens on those lines. 调试器本身看不到这些行,因为在这些行上没有发生任何有趣的事情。

Line 18 is a type declaration, p2 on line 19 is interesting because there the reference value for p2 is allocated and assigned the value after the =. 第18行是类型声明,第19行的p2很有趣,因为在那里分配了p2的参考值,并在=之后分配了值。

Then it skips to the next execution and that's line 26. 然后跳到下一个执行,即第26行。

You spread it out over the multiple lines, but the debugger runs off compiled code, that has certain "hooks" in them that point to the position in the original source code. 您将其分布在多行中,但是调试器运行的是已编译的代码,其中包含某些“钩”,这些“钩”指向原始源代码中的位置。

Normally you'd code this in a single line, and that's how the debuggers are set up. 通常,您会在一行中编写代码,这就是调试器的设置方式。 If you wish to see more details use the step into option instead of step over. 如果您想查看更多详细信息,请使用“进入”选项,而不是“跳过”。

Basically a debugger shows you points in the code where things change and states get updated. 基本上,调试器会向您显示代码中发生变化和状态更新的点。 When the debugger is paused on those lines you can check what values of variables are and the state of objects, etc... It does not see any other things or artifacts in the code you've introduced, only when new things are created, values assigned or changed. 当调试器在这些行上暂停时,您可以检查变量的值以及对象的状态等。它不会在您引入的代码中看到任何其他事物或工件,只有在创建新事物时,分配或更改的值。 Only the interesting stuff, at the point where it happens, so you can analyze values that are passed on to a method, values returned from a method, a new instantiated class state, etc... to see if everything is right as rain. 在发生事件时,只有有趣的东西,因此您可以分析传递给方法的值,从方法返回的值,新的实例化类状态等,以查看是否一切如雨后春笋。

Ask yourself, what value would I gain if the debugger paused on line 24? 问问自己, what value would I gain if the debugger paused on line 24? Absolutely nothing. 绝对没有。 The interesting part happens right before p2 is assigned and after p2 is assigned, or in the constructor of Player() for which you can choose step into, or when an Exception is thrown. 有趣的部分发生在分配p2之前和分配p2之后,或者发生在Player()的构造函数中,您可以为其选择单步执行,或者抛出Exception

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM