Title: Programming Languages and Paradigms
1Programming Languages and Paradigms
- Activation Records in Java
2Activation Records in Java
- Activation record A chunk of computer memory
which contains the data needed for the activation
of a routine - Java Virtual Machines Role
- Loads class files needed and executes bytecodes
they contain in a running program - Organizes memory into structured areas
3Java Virtual Machine
4Java 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
5Runtime 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
6Objects Representation in Heap
7Runtime Data Areas Java Stack
- Java Stack
- Each thread creates separate Java stack
- Contains frames current threads state
- Pushing and popping of frames
8Stack 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
9Example 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
10Example Contiguous Stack
11Bank 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// 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// Driver.java public static void main( String
args ) BankAccount a new BankAccount() Bank
Account b new BankAccount() b.deposit( 100
)
Parameters
args0
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// BankAccount.java private double
balance private static int totalAccounts
0 public BankAccount() balance
0 totalAccounts public void deposit(
double amount ) balance amount
Parameters
args0
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// Driver.java public static void main( String
args ) BankAccount a new BankAccount() Bank
Account b new BankAccount() b.deposit( 100
)
Parameters
args0
The pointer is returned to the calling frame
a
b
Local variables
main()
Frame data
Operand stack
100
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// Driver.java public static void main( String
args ) BankAccount a new BankAccount() Bank
Account b new BankAccount() b.deposit( 100
)
Parameters
args0
a
b
Local variables
A pointer to the BankAccount pointer in the heap
is created (Constant Pool Resolution)
main()
Frame data
Operand stack
100
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// BankAccount.java private double
balance private static int totalAccounts
0 public BankAccount() balance
0 totalAccounts public void deposit(
double amount ) balance amount
Parameters
args0
a
b
Local variables
main()
Frame data
Operand stack
100
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// Driver.java public static void main( String
args ) BankAccount a new BankAccount() Bank
Account b new BankAccount() b.deposit( 100
)
Parameters
args0
a
b
Local variables
main()
Frame data
Operand stack
100
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// Driver.java public static void main( String
args ) BankAccount a new BankAccount() Bank
Account b new BankAccount() b.deposit( 100
)
Parameters
args0
a
b
b
Local variables
main()
Frame data
100
Operand stack
amount100
100 is popped from the operand stack and put into
the next frames 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
balance
Static Variables totalAccounts
0.0
2
20// BankAccount.java private double
balance private static int totalAccounts
0 public BankAccount() balance
0 totalAccounts public void deposit(
double amount ) balance amount
Parameters
args0
a
b
Local variables
main()
Frame data
Operand stack
Parameters
amount100
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
balance
Static Variables totalAccounts
The frame knows which balance to modify because
of the object reference
0.0
2