23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Keep<strong>in</strong>g Track of the Program Counter<br />

The JVM keeps a special variable, called the program counter, to ma<strong>in</strong>ta<strong>in</strong> the<br />

address of the statement the JVM is currently execut<strong>in</strong>g <strong>in</strong> the program. When a<br />

method "cool" <strong>in</strong>vokes another method "fool", the current value of the<br />

program counter is recorded <strong>in</strong> the frame of the current <strong>in</strong>vocation of cool (so<br />

the JVM will know where to return to when method fool is done). At the top of<br />

the <strong>Java</strong> stack is the frame of the runn<strong>in</strong>g method, that is, the method that<br />

currently has control of the execution. The rema<strong>in</strong><strong>in</strong>g elements of the stack are<br />

frames of the suspended methods, that is, methods that have <strong>in</strong>voked another<br />

method <strong>and</strong> are currently wait<strong>in</strong>g for it to return control to them upon its<br />

term<strong>in</strong>ation. The order of the elements <strong>in</strong> the stack corresponds to the cha<strong>in</strong> of<br />

<strong>in</strong>vocations of the currently active methods. When a new method is <strong>in</strong>voked, a<br />

frame for this method is pushed onto the stack. When it term<strong>in</strong>ates, its frame is<br />

887

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!