programming languages and paradigms

20
Programming Languages and Paradigms Activation Records in Java

Upload: erv

Post on 06-Jan-2016

37 views

Category:

Documents


1 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Programming Languages and Paradigms

Programming Languages and Paradigms

Activation Records in Java

Page 2: Programming Languages and Paradigms

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

Page 3: Programming Languages and Paradigms

Java Virtual Machine

Page 4: Programming Languages and Paradigms

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

Page 5: Programming Languages and Paradigms

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

Page 6: Programming Languages and Paradigms

Objects Representation in Heap

Page 7: Programming Languages and Paradigms

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

Runtime Data Areas: Java Stack

Page 8: Programming Languages and Paradigms

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 2Incoming parameter 1

Frame data

Local variables

Operand stack

Outgoing parameters

Current frame

Next frame

Stack Frame

Page 9: Programming Languages and Paradigms

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

Page 10: Programming Languages and Paradigms

Example: Contiguous Stack

Page 11: Programming Languages and Paradigms

Bank Account Examplepublic 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 );

}

}

Page 12: Programming Languages and Paradigms

Method Area Heap

Driver class

Constant Pool“BankAccount” a“BankAccount” b

100

Methodsmain()

// In command promptjava Driver

// In Java Virtual MachineDriver.main( args )

A stack frame for main() is pushed

into the Java stack

main()

Parameters

Local variables

Frame data

Operand stack

*

100

args[0] …

a b

ClassLoader loads Driver to the method

area

Page 13: Programming Languages and Paradigms

Method Area Heap

Driver class

Constant Pool“BankAccount” a“BankAccount” b

100

Methodsmain()

main()

Parameters

Local variables

Frame data

Operand stack

*

100

args[0] …// Driver.javapublic 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 Pool0

MethodsBankAccount()

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)

a b

ClassLoader loads BankAccount to the

method area

Page 14: Programming Languages and Paradigms

Method Area Heap

Driver class

Constant Pool“BankAccount” a“BankAccount” b

100

Methodsmain()

pointer

balance

0.0

BankAccount class

Constant Pool0

MethodsBankAccount()

deposit( double )

Static VariablestotalAccounts

0

main()

BankAccount()

Parameters

Local variables

Frame data

Operand stack

Parameters

Frame data

Parameters

*

*

Operand stack

args[0] …

Local variables

// BankAccount.javaprivate 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

a b

Page 15: Programming Languages and Paradigms

Method Area Heap

Driver class

Constant Pool“BankAccount” a“BankAccount” b

100

Methodsmain()

main()

Parameters

Local variables

Frame data

Operand stack

*

args[0] …// Driver.javapublic 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 Pool0

MethodsBankAccount()

deposit( double )

pointer

*

Static VariablestotalAccounts

1

*

The stack frame for the BankAccount

constructor is popped from the

Java stack

a b

balance

0.0

100

The pointer is returned to the calling frame

The pointer is popped from the operand stack

and assigned to a

Page 16: Programming Languages and Paradigms

Method Area Heap

Driver class

Constant Pool“BankAccount” a“BankAccount” b

100

Methodsmain()

main()

Parameters

Local variables

Frame data

Operand stack

*

args[0] …// Driver.javapublic 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 Pool0

MethodsBankAccount()

deposit( double )

A pointer to the BankAccount class

data is created

pointer

balance

0.0

pointer

*

Static VariablestotalAccounts

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

a b

Page 17: Programming Languages and Paradigms

Method Area Heap

Driver class

Constant Pool“BankAccount” a“BankAccount” b

100

Methodsmain()

balance

0.0

BankAccount class

Constant Pool0

MethodsBankAccount()

deposit( double )

Static VariablestotalAccounts

1

main()

BankAccount()

Parameters

Local variables

Frame data

Operand stack

Parameters

Frame data

Parameters

*

Operand stack

args[0] …

Local variables

// BankAccount.javaprivate 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

a b

The balance variable of this instance is initialized with a

default value

The balance variable of this instance is assigned with 0

Page 18: Programming Languages and Paradigms

Method Area Heap

Driver class

Constant Pool“BankAccount” a“BankAccount” b

100

Methodsmain()

main()

Parameters

Local variables

Frame data

Operand stack

*

args[0] …// Driver.javapublic 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 Pool0

MethodsBankAccount()

deposit( double )

*

Static VariablestotalAccounts

2

*

pointer

balance

0.0

balance

0.0

pointer

100

a b

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

Page 19: Programming Languages and Paradigms

Method Area Heap

Driver class

Constant Pool“BankAccount” a“BankAccount” b

100

BankAccount class

Constant Pool0

Methodsmain() Methods

BankAccount()deposit( double ) pointer

pointer

balance

0.0

balance

0.0

Static VariablestotalAccounts

2

main()

Parameters

Local variables

Frame data

Operand stack

*

args[0] …// Driver.javapublic 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

a b

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

Page 20: Programming Languages and Paradigms

Method Area Heap

Driver class

Constant Pool“BankAccount” a“BankAccount” b

100

BankAccount class

Constant Pool0

Methodsmain() Methods

BankAccount()deposit( double ) pointer

pointer

balance

100.0

balance

0.0

Static VariablestotalAccounts

2

main()

BankAccount()

Parameters

Local variables

Frame data

Operand stack

Parameters

Frame data

Parameters

*

Operand stack

args[0] …// BankAccount.javaprivate 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

a b

b

amount=100

0.0