1. Trace variable declaration and assignment
    public class Foo { public static void main(String[] args) { int x = 3; System.out.println("Now x = " + x); x += 5; System.out.println("And now x = " + x); } }

    Trace:

     

    Method: 'main'

     
    Line Action x
    2 start ---
    3 set x to 3 3
    4 print "Now x = 3"  
    5 set x to 8 8
    6 print "And now x = 8"  
    7 return  

     

  2. Trace multiple variable declarations
    public class Foo { public static void main(String[] args) { int x = 3; System.out.println("Here x = " + x); x += 5; int y = 4; System.out.println("And here x,y = " + x + "," + y); } }

    Trace:

     

    Method: 'main'

       
    Line Action x y
    2 start --- ---
    3 set x to 3 3  
    4 print "Here x = 3"    
    5 set x to 8 8  
    6 set y to 4   4
    7 print "And here x,y = 8,4"    
    8 return    

     

  3. Trace multiple steps on one line
    public class Foo { public static void main(String[] args) { int x = 3, y = 4; System.out.println("Here x,y = " + x + "," + y); y = 2 * (x = 5); System.out.println("And here x,y = " + x + "," + y); } }

    Trace:

     

    Method: 'main'

       
    Line Action x y
    2 start --- ---
    3 set x to 3 3  
    3 set y to 4   4
    4 print "Here x,y = 3,4"    
    5 set x = 5 5  
    5 set y = 10   10
    6 print "And here x,y = 5,10"    
    7 return    

     

  4. Trace a conditional
    public class Foo { public static void main(String[] args) { int i = -3; if (i > 0) System.out.println(i + " is positive"); else if (i == 0) System.out.println(i + " is zero"); else System.out.println(i + " is negative"); } }

    Trace:

     

    Method: 'main'

     
    Line Action i
    2 start ---
    3 set i to -3 -3
    4 test if (-3 > 0)  
    6 test if (-3 == 0)  
    9 print "-3 is negative"  
    10 return  

     

  5. Trace a loop
    public class Foo { public static void main(String[] args) { String s = "ab"; for (int i=0; i<s.length(); i++) System.out.println(s.charAt(i)); } }

    Trace:

     

    Method: 'main'

       
    Line Action s i
    2 start --- ---
    3 set s to "abc" "abc"  
    4 set i to 0   0
    4 test if (0 < 2)    
    5 print "a"    
    4 set i to 1   1
    4 test if (1 < 2)    
    5 print "b"    
    4 set i to 2   2
    4 test if (2 < 2)    
    6 return    

     

  6. Trace a method call
     
    1. Without source code
      public class Foo { public static void main(String[] args) { int x = 3, y = 4; int lesser = Math.min(x,y); System.out.println("lesser = " + lesser); } }

      Trace:

       

      Method: 'main'

           
      Line Action x y lesser
      2 start --- --- ---
      3 set x to 3 3    
      3 set y to 4   4  
      4 call Math.min(3,4)
      returns 3
           
      4 set lesser to 3     3
      5 print "lesser = 3"      
      6 return      

      Another Example:

      public class Foo { public static void main(String[] args) { int x = 3, y = 4; int z = Math.min(x, (int)Math.sqrt(y)); System.out.println("z = " + z); } }

      Trace:

       

      Method: 'main'

           
      Line Action x y z
      2 start --- --- ---
      3 set x to 3 3    
      3 set y to 4   4  
      4 call Math.sqrt(4)
      returns 2.0
           
      4 call Math.min(3,2)
      returns 2
           
      4 set z to 2     2
      5 print "z = 2"      
      6 return      

       

    2. With source code
      public class Foo { public static int times(int x, int y) { return x*y; } public static void main(String[] args) { int x = 3, y = 4; int z = times(x,y); System.out.println("z = " + z); } }

      Trace:

       

      Method: 'main'

           
      Line Action x y z
      6 start --- --- ---
      7 set x to 3 3    
      7 set y to 4   4  
      8 call times(3,4)      
       
       

      Method: 'times'

         
      Line Action x y
      2 start 3 4
      3 return 12    
      8 returns 12      
      8 set z to 12     12
      9 print "z = 12"      
      10 return