简体   繁体   中英

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

I would like to know the execution process from Line 18 till 30.

For example when I debug the code (starting now from Line 11) it goes step by step so:

Line11 There it executes the method and after that it goes to

Line13 because it is an comment. 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. Calls the default-Constructor gives and reference (pointer named as p1) the type of Player and puts the Playerobject into the Player. Endofline now it goes to

Line 15 it executes. Now it goes to

Line 17 its a comment and then it goes to

Line 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.

After Line19 it skips the other lines and goes to

Line 26 strange -> where are the lines 18,22-25 when do it execute this lines. And how/when would the debugger recognizes when i have forgot an semicolon on 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. 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 =.

Then it skips to the next execution and that's line 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? 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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