324-lecture21 - Principles Principles of Programming...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Principles of Programming Languages Lecture 21 University of Toronto Wael Aboulsaadat wael@cs.toronto.edu http://portal.utoronto.ca/ 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Components of an Imperative Language Data types Iteration construct University of Toronto Branching construct b Subprogram construct Container construct 2
Background image of page 2
Implementation models: – Pass by value – Pass by result – Pass by value-result – Pass by reference odern languages support pass y alue and pass y ference Subprograms: parameter passing University of Toronto Modern languages support pass-by-value and pass-by-reference Concurrency problems with other models. 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Pass by Value-Result Subprograms: parameter passing public class Counters extends Thread { public int m = 5; public int n = 3; public void increment(int k, int j) { k = k + 1; j = j + 2; } public class User extends Thread { public Counters _counters; public User( Counters counters ) { _counters = counters; } public void run( ) { t = _counters.m + _counters.n; University of Toronto 4 public void run( ) { increment( m , n ); } } int x = _counters.m + _counters.n; } } public class App { public static void main( String strarrArgs[] ) { Counters counters = new Counters( ); User user = new User( counters ); counters.start( ); user.start( ); } } Result is either 5/3 or 6/5 or 5/5 !!
Background image of page 4
Java – Primitives: pass by value – Mutable Objects: pass by reference • Vector, List, Map, Hashtable, StringBuffer – Immutable objects: pass by value • String! Subprograms: parameter passing void dothis(String str){ String strInput = str; } …. lTime1 = System.currentTimeMillis(); for(int nIndex=0;nIndex < 1000000;nIndex++){ String strVal = "alkasdfasdfasdfdjflkasdjfl"; dothis(strVal); } lTime1 = System.currentTimeMillis(); University of Toronto Most modern languages are following Java’s model. Always check if the new language you are learning is passing objects by value or reference (and can you chose between them as in C/C++) 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Scope: introduction The textual region of the program in which a specific set of variable bindings are active is called the scope . Variables and Scope: – A variable is said to be visible in a statement if it can be referenced in that statement without a type error University of Toronto If a variable outlives its binding it's garbage – If a binding outlives a variable it's a dangling reference Elaboration: – It is the process of opening a new scope and creating appropriate bindings. – Done upon entering a subroutine 6
Background image of page 6
A block is a section of code in which local variables are allocated and de-allocated at the start/end of the block.
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 08/02/2011.

Page1 / 32

324-lecture21 - Principles Principles of Programming...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online