1 / 20

Programming Languages and Paradigms

Programming Languages and Paradigms. Activation Records in Java. Activation Records in Java. Activation record: A chunk of computer memory which contains the data needed for the activation of a routine Java Virtual Machine ’ s Role

erv
Download Presentation

Programming Languages and Paradigms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming Languages and Paradigms Activation Records in Java

  2. Activation Records in Java • 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. Java Virtual Machine • 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. Runtime Data Areas: Java Stack • Java Stack • Each thread creates separate Java stack • Contains frames: current thread’s state • Pushing and popping of frames

  8. Incoming parameter 2 Incoming parameter 1 Local variables Current frame Frame data Operand stack Outgoing parameters Next frame Stack Frame • 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

  9. Example: Separate Stack Frames 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; } }

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

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

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

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

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

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

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

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

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

More Related