Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Languages and Paradigms Activation Records in Java.

Similar presentations


Presentation on theme: "Programming Languages and Paradigms Activation Records in Java."— Presentation transcript:

1 Programming Languages and Paradigms Activation Records in Java

2 Activation record: A chunk of computer memory which contains the data needed for the activation of a routine Java Virtual Machine ’ s Role Loads class files needed and executes bytecodes they contain in a running program Organizes memory into structured areas

3 Java Virtual Machine

4 Runtime data areas in JVM Method area – contains class information Heap – memory for class instances and arrays PC registers – program counters for each thread Java stack – stores stack frames Native method stacks – Native methods: written in other languages

5 Runtime Data Areas Method Area Contains class information One for each JVM instance Shared by all threads in JVM One thread access at a time Heap Contains class instance or array (objects) One for each JVM instance Facilitates garbage collection Expands and contracts as program progresses

6 Objects Representation in Heap

7 Java Stack Each thread creates separate Java stack Contains frames: current thread ’ s state Pushing and popping of frames Runtime Data Areas: Java Stack

8 Local Variables Organized in an array Accessed via array indices Operand Stack Organized in an array Accessed via pushing and popping Always on top of the stack frame Work space for operations Frame Data Constant Pool Resolution: Dynamic Binding Normal Method Return No exception thrown Returns a value to the previous frame Exception Dispatch … Incoming parameter 2 Incoming parameter 1 Frame data Local variables Operand stack Outgoing parameters Current frame Next frame Stack Frame

9 class StackFrameExample { public static void addAndPrint() { double result = addTwoTypes(1, 88.88); System.out.println(result); } public static double addTwoTypes(int i, double d) { return i + d; } Example: Separate Stack Frames

10 Example: Contiguous Stack

11 Bank Account Example public class BankAccount { private double balance; public static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++; } public void deposit( double amount ) { balance += amount; } public class Driver { public static void main( String[] args ) { BankAccount a = new BankAccount(); BankAccount b = new BankAccount(); b.deposit( 100 ); }

12 Method Area Heap Driver class Constant Pool “BankAccount” a “BankAccount” b 100 Methods main() // In command prompt java Driver // In Java Virtual Machine Driver.main( args ) A stack frame for main() is pushed into the Java stack main() Parameters Local variables Frame data Operand stack * 100 args[0]… ab ClassLoader loads Driver to the method area

13 Method Area Heap Driver class Constant Pool “BankAccount” a “BankAccount” b 100 Methods main() Parameters Local variables Frame data Operand stack * 100 args[0]… // Driver.java public static void main( String[] args ) { BankAccount a = new BankAccount(); BankAccount b = new BankAccount(); b.deposit( 100 ); } A stack frame for the BankAccount constructor is pushed into the Java stack Parameters BankAccount() Parameters Frame data Parameters Operand stack Local variables BankAccount class Constant Pool 0 Methods BankAccount() deposit( double ) pointer * A pointer to the BankAccount class data is created A pointer to the BankAccount pointer in the heap is created (Constant Pool Resolution) ab ClassLoader loads BankAccount to the method area

14 Method Area Heap Driver class Constant Pool “BankAccount” a “BankAccount” b 100 Methods main() pointer balance 0.0 BankAccount class Constant Pool 0 Methods BankAccount() deposit( double ) Static Variables totalAccounts 0 main() BankAccount() Parameters Local variables Frame data Operand stack Parameters Frame data Parameters * * Operand stack args[0]… Local variables // BankAccount.java private double balance; private static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++; } public void deposit( double amount ) { balance += amount; } The balance variable of this instance is initialized with a default value The totalAccounts static variable of BankAccount is initialized with a default value and then assigned with 0 The balance variable of this instance is assigned with 0 totalAccounts is incremented by 1 1 100 ab

15 Method Area Heap Driver class Constant Pool “BankAccount” a “BankAccount” b 100 Methods main() Parameters Local variables Frame data Operand stack * args[0]… // Driver.java public static void main( String[] args ) { BankAccount a = new BankAccount(); BankAccount b = new BankAccount(); b.deposit( 100 ); } Parameters BankAccount() Parameters Frame data Parameters Operand stack Local variables BankAccount class Constant Pool 0 Methods BankAccount() deposit( double ) pointer * Static Variables totalAccounts 1 * The stack frame for the BankAccount constructor is popped from the Java stack ab balance 0.0 100 The pointer is returned to the calling frame The pointer is popped from the operand stack and assigned to a

16 Method Area Heap Driver class Constant Pool “BankAccount” a “BankAccount” b 100 Methods main() Parameters Local variables Frame data Operand stack * args[0]… // Driver.java public static void main( String[] args ) { BankAccount a = new BankAccount(); BankAccount b = new BankAccount(); b.deposit( 100 ); } A stack frame for the BankAccount Constructor is pushed into the Java stack Parameters BankAccount() Parameters Frame data Parameters Operand stack Local variables BankAccount class Constant Pool 0 Methods BankAccount() deposit( double ) A pointer to the BankAccount class data is created pointer balance 0.0 pointer * Static Variables totalAccounts 1 100 A pointer to the BankAccount pointer in the heap is created (Constant Pool Resolution) Since the BankAccount class was already loaded in the method area, no other loading happens ab

17 Method Area Heap Driver class Constant Pool “BankAccount” a “BankAccount” b 100 Methods main() balance 0.0 BankAccount class Constant Pool 0 Methods BankAccount() deposit( double ) Static Variables totalAccounts 1 main() BankAccount() Parameters Local variables Frame data Operand stack Parameters Frame data Parameters * Operand stack args[0]… Local variables // BankAccount.java private double balance; private static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++; } public void deposit( double amount ) { balance += amount; } pointer balance 0.0 pointer * Nothing happens since the totalAccounts was already initialized when the BankAccount class was first loaded totalAccounts is incremented by 1 2 100 ab The balance variable of this instance is initialized with a default value The balance variable of this instance is assigned with 0

18 Method Area Heap Driver class Constant Pool “BankAccount” a “BankAccount” b 100 Methods main() Parameters Local variables Frame data Operand stack * args[0]… // Driver.java public static void main( String[] args ) { BankAccount a = new BankAccount(); BankAccount b = new BankAccount(); b.deposit( 100 ); } Parameters BankAccount() Parameters Frame data Parameters Operand stack Local variables BankAccount class Constant Pool 0 Methods BankAccount() deposit( double ) * Static Variables totalAccounts 2 * pointer balance 0.0 balance 0.0 pointer 100 ab The stack frame for the BankAccount Constructor is popped from the Java stack The pointer is popped from the operand stack and assigned to b The pointer is returned to the calling frame

19 Method Area Heap Driver class Constant Pool “BankAccount” a “BankAccount” b 100 BankAccount class Constant Pool 0 Methods main() Methods BankAccount() deposit( double ) pointer balance 0.0 balance 0.0 Static Variables totalAccounts 2 main() Parameters Local variables Frame data Operand stack * args[0]… // Driver.java public static void main( String[] args ) { BankAccount a = new BankAccount(); BankAccount b = new BankAccount(); b.deposit( 100 ); } Parameters deposit( double ) Parameters Frame data Parameters Operand stack Local variables b The object reference to the instance is always put as the first local variable of a stack frame 100 ab b amount=100 A stack frame for the deposit method of instance ‘b’ is pushed into the Java stack 100 is popped from the operand stack and put into the next frame’s parameters

20 Method Area Heap Driver class Constant Pool “BankAccount” a “BankAccount” b 100 BankAccount class Constant Pool 0 Methods main() Methods BankAccount() deposit( double ) pointer balance 100.0 balance 0.0 Static Variables totalAccounts 2 main() BankAccount() Parameters Local variables Frame data Operand stack Parameters Frame data Parameters * Operand stack args[0]… // BankAccount.java private double balance; private static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++; } public void deposit( double amount ) { balance += amount; } Local variables The frame knows which balance to modify because of the object reference ab b amount=100 0.0


Download ppt "Programming Languages and Paradigms Activation Records in Java."

Similar presentations


Ads by Google