Chapter 5 Names, Bindings, Type Checking, and Scopes Names Variables The Concept of Binding Scope and Lifetime Type Checking Referencing Environments Named Constants Names Used for variables, subprograms (or methods), types, classes, etc. Different languages have different rules for the formation of identifiers. Java: an unlimited-length sequence of letters and digits, the first one must be a letter. Fortran77: only be 1-6 letters or digits, the first of which must be a letter. Fortran90: up to 31 characters long, and allow them to include the _ character. 1
Prolog: starts with an upper-case letter is a variable, begins with a lower-case letter is a literal (constant) symbol. Perl: The most basic kind of variable in Perl is the scalar variable. Scalar variables hold both strings and numbers, and are remarkable in that strings and numbers are completely interchangable. -- The name of a scalar variable must start with a $, $priority = 9; $priority = 'high'; -- The name of a variable whose value is an array must start with an @, @monthtable 2
Case sensitivity Uppercase and lowercase letters in names are distinct. int var1, Var1; C, C++, JavaScript and Java names are case sensitive -- Visual Basic, Pascal and ASP are not case sensitive. Advantage: accurate and disambiguous. Disadvantage: readability (names that look alike are different) Keywords and Reserved Words A keyword is a word that is special only in certain contexts, e.g., in Fortran Real VarName (Real is a data type followed with a name) Real = 3.4 (Real is a variable) A reserved word is a special word that cannot be used as a user-defined name -- Reserved words are better than keywords because the ability to redefine keywords can be confusing. 3
Variables An abstraction of a memory cell. Variable attributes: Name Address Value Type Scope Lifetime Name More than one variable might have the same name. for (int i = 0; i < 10; i++) { a[i] = i;} for (int i = 0; i < 100; i++) { squares[i] = i*i; } 4
Address Address - the memory address with which a variable is associated A variable may have different addresses at different times during execution If subprogram has a local variable that is allocated when the subprogram is called, different calls may result in that variable have different addresses. If two variable names can be used to access the same memory location, they are called aliases Aliases can be created in different ways Reference variables, Pointers Pascal variant records C and C++ unions, FORTRAN EQUIVALENCE parameter passing 5
Referernce aliases String Name1= new String( Steve Jobs ); String Name2= new String( Steve Wozniak ); name1 name2 name2 = name1; name1 name2 Steve Jobs Steve Wozniak Steve Jobs Steve Wozniak 6
Pointer aliases. If several pointers point into the same block of memory, char *p = malloc(strlen( Java Software )); char *p2 = malloc(strlen( Solution )); p2 = p; Parameter passing by reference or pointer X Y null null Example 1: immutable string If the whole object cannot be extended by another class, the object is called immutable Object x = Java Solution ; givemeastring (x); System.out.println (x); [...] void givemeastring (Object y) { y = "This is a string"; } Result: Java Solution Object x = null; givemeastring (x); System.out.println (x); [...] void givemeastring (Object y) { y = "This is a string"; } Result: null 7
Example 2: mutable object class Car { String licenseplate; double speed; double maxspeed; void SetlicensePlate(String licenceplate1 ) { licenceplate = licenceplate1; } double GetSpeed () { return speed; } void SetSpeed (int speed1) { speed =speed1; } } class CarTest { public static void main(string args[]) { Car c = new Car(); Car d = c; d.setlicenseplate( Toronto66 ); d.setspeed (150); Aliases are harmful to readability It allows a variable to have its value changed by an assignment to a different variable. Program readers must remember all of them. 8
Value The contents of the memory cell at the variable's address. Are the two occurrences of a in this expression the same? a := a + 1; lvalue: The one on the left of the assignment refers to the location of the variable whose name is a; rvalue: The one on the right of the assignment refers to the value of the variable whose name is a; Most variables are allowed to change their value binding. #include <stdio.h> int main() { int x=10; x=5; printf("x = %d ", x); return 0; } 9
Constants are variables that are not allowed to do so. class CircleStuff { static final float PI = 3.1416;... } The final keyword final variables: a constant which will not and cannot change. final methods: it cannot be overridden. final arguments: the method will not directly change them final classes: this class will not be subclassed. Example: public final class String public final class FinalMethod { private final String name; protected FinalMethod(final String name) { this.name = name; } public final String getname() { return name; } } 10
Type the range of values the variable can have; the operations that are defined for those values; int signed integers 32 bits -2147483648 to 2147483647 Arithmetic operations + (addition), - (subtraction), * (multiplication), / (division), and % (modulo). The Concept of Binding A binding is an association, such as between an attribute and an entity, or between an operation and a symbol Car c = new Car(); int speed1= c.getspeed(); 11
Binding time is the time at which a binding takes place. Possible Binding Times Static binding: before run time and remain unchanged. Language design time -- bind operator symbols to operations, bind + to addition operation Compile time Dynamic binding: occur during run time or can change Runtime int i; // i1... i = j+4; // i2 At compile time, i2 is bound to the i1 (its declaration). At run time, i is bound to the value computed for the expression j+4. 12
Shape draw () erase () Circle draw() erase () Triangle draw() erase () Line draw() erase () Shape s = new Circle(); s.draw(); Shape s = new Line(); s.draw(); Shape s = new Triangle(); s.draw(); 13
Bindings and Lifetime A variable may be bound to an address on the stack, or on the heap. Java: All data of primitive data type reside on stack All objects reside on heap int i = 6, int j ; int [ ] a = { 1,3,5,7,9}; int [ ] b = new int [3] ; String s = "abcdef String t = null; 14