The Concept of Binding (1) The Concept of Binding (2)

advertisement
:: INT304 ::
Programming Syntax & Semantics
The Concept of Binding
(1)
:: INT304 ::
Programming Syntax & Semantics
The Concept of Binding
(2)
 Definition:
A binding is an association, such as between an
attribute and an entity, or between an
operation and a symbol
 Definition:
A binding is static if it first occurs before run
time and remains unchanged throughout
program execution.
 Definition:
Binding time is the time at which a binding
takes place.
 Def: A binding is dynamic if it first occurs
during execution or can change during
execution of the program.
Possible binding times:
- Language design time
e.g., bind operator symbols to operations
- Language implementation time
e.g., bind fl. pt. type to a representation
- Compile time
e.g., bind a variable to a type in C or Java
- Load time
e.g., bind a FORTRAN 77 variable to a memory cell (or a C static variable)
- Runtime
e.g., bind a non-static local variable to a memory cell
© Copyright Khaitong-2-5-0-9. 2003
Page: 1 of 12
© Copyright Khaitong-2-5-0-9. 2003
Page: 2 of 12
:: INT304 ::
Programming Syntax & Semantics
Type Binding
(1)
 How is a type specific?
Type Binding
 If static, the type may be specified by either an
explicit or an implicit declaration
~ Specified through an assignment statement
e.g. JavaScript
o list = [2, 4.33, 6, 8] ;
o list = 17.3 ;
~ Def: An explicit declaration is a program
statement used for declaring the types of variables
~ Advantage:
~ Def: An implicit declaration is a default
mechanism for specifying types of variables (the
first appearance of the variable in the program)
~ Disadvantages:
~ FORTRAN, PL/I, BASIC, and Perl provide implicit
declarations
FORTRAN77
Sum = 0
READ *, ListLen
DO 10 ICOUNT = 0, ListLen
READ *, InList(icount)
SUM= Sum + InList(iCount)
Continue
ICount = 29
Implicit Declaration
(โดยนัย: ไมตองประกาศ)
© Copyright Khaitong-2-5-0-9. 2003
o flexibility (generic program units)
o High cost (dynamic type checking and
interpretation)
o Type error detection by the compiler is difficult
VB:
Name = “Peter”
FulltTmeSalary = 12590
PartTimeSalary = 25840
FulltTmeSalary = Name
IF FullTime < PartTime Then
C Language
Explicit
Declaration
(2)
 Dynamic Type Binding (VB, JavaScript, PHP)
 When does the binding take place?
10
:: INT304 ::
Programming Syntax & Semantics
Static type binding: Compile Time Error
Dynamic type binding: Run-time Error
(Type Checking be done at runtime)
double sum = 0 ;
int list_len, icount ;
scanf(“%d”, &list_len);
for icount = 0,i<list_len;i++) {
scanf(“%d”, inlist(icount);
sum = sum + inlist(icount);
}
sum = “125,000.00” ;
Page: 3 of 12
© Copyright Khaitong-2-5-0-9. 2003
Page: 4 of 12
:: INT304 ::
Programming Syntax & Semantics
Storage Binding & Life time
(1)
 Allocation: getting a cell from some pool of
available cells
 De-allocation: putting a cell back into the pool
 Def: The lifetime of a variable is the time
during which it is bound to a particular
memory cell
~ Static
~ Dynamic
:: INT304 ::
Programming Syntax & Semantics
Storage Binding & Life time
(2)
 Stack-Dynamic
~ Storage bindings are create for variables when
their declaration statements are elaborated.
 Explicit Heap-Dynamic
~ Allocated and deallocated by explicit directives,
specified by the programmer, which take effect
during execution
 Implicit Heap-Dynamic
o Stack-Dynamic
o Explicit Heap-Dynamic
o Implicit Heap-Dynamic
~ Allocation and deallocation caused by assignment
statements
Static:
- bound to memory cells before execution begins and remains bound to the same
memory cell throughout execution. e.g.
all FORTRAN 77 variables, C static variables
Advantages:
- efficiency (direct addressing), history-sensitive subprogram support
Disadvantage:
- Lack of flexibility (no recursion)
Stack-Dynamic:
- All attributes except address are statically bound
e.g. local variables in C subprograms and Java methods
Advantage: allows recursion; conserves storage
Disadvantages: Overhead of allocation and deallocation
Subprograms cannot be history sensitive
Explicit Heap Dynamic:
- Referenced only through pointers or references
e.g. dynamic objects in C++ (via new and delete), all objects in Java
Advantage: provides for dynamic storage management
Disadvantage: inefficient and unreliable
Implicit Heap Dynamic:
- All strings and arrays in Perl and JavaScript
Advantage: flexibility
Disadvantages: Inefficient, because all attributes are dynamic
Loss of error detection
© Copyright Khaitong-2-5-0-9. 2003
Page: 5 of 12
© Copyright Khaitong-2-5-0-9. 2003
Page: 6 of 12
:: INT304 ::
Programming Syntax & Semantics
Arrays
:: INT304 ::
Programming Syntax & Semantics
Multidimensional Array:
 Arrays are objects in Java. They are bounds
checked and provide access to length “variable”
arr
 Primitive array
int[] arr = new int[5];
for (int i=0; i<arr.length; i++)
arr[i] = i * i ; //value
length
5
0
1
4
9
16
 Declaration &
Instantiate
length
0
4
0
0
length
5
0
0
1
2
3
5
0
0
9
0
0
0
length
table[1][3] = 9;
table[3][0] = 1;
Date[] od = new Date[6];
for (int i=0; i<od.length; i++)
od[i] = new Date(); //ref
od
table
int [][] table ;
table = new int[4][5]
 Object array
(1)
0 1 2 3 4
0
0
0
0
0
0
9
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
length
5
1
0
0
length
6
© Copyright Khaitong-2-5-0-9. 2003
Page: 7 of 12
© Copyright Khaitong-2-5-0-9. 2003
Page: 8 of 12
:: INT304 ::
Programming Syntax & Semantics
Multidimensional Array:
 Declaration &
Instantiate
tb
length
new
new
new
new
 Initializer List
3
length
4
=
=
=
=
Array Initialization:
(1)
table
length
1
Student []tb[] ;
tb = new Student[4];
tb[0]
tb[1]
tb[2]
tb[3]
(1)
:: INT304 ::
Programming Syntax & Semantics
Student[3]
Student[5]
Student[6]
Student[4]
0
1
2
3
6
length
tb[2][5] = new Student()
0
3
length
5
9
0
1
2
3
0 1 2 3 4 5
id
name
4
{ {1,2,3,4,0},
{5,6,7,8,0},
{9,9,9,9,9},
{0,0,0,0,0} }
4
2
4
0
9
9
0
0
length
int [][] table =
length
5
0 1 2 3 4
1
2
3
4
0
5
6
7
8
0
9
9
9
9
9
0
0
0
0
0
9
9
length
5
0
0
0
Summary:
- Array ในภาษาจาวา เปน Object
- Array อาจจะมีตัวชี้หลายไดหลายตัว
- Array เพิ่มขนาด (จํานวนชอง) ไมได (สรางใหมได แตของเดิมหาย)
- index (ดัชนี) ของภาษาจาวา ตองเปน จํานวนเต็ม ที่อยูระหวาง 0 – (length-1)
ถาอางถึงชองที่อยูเกินขอบเขต จะเกิด Exception: ArrayIndexOutOfBoundsException
- จาวาจะตั้งคาเริ่มตนของ Array โดยอัตโนมัติ
- เราสามารถรูข
 นาดของ Array ไดจาก attribute length ของ Array
(ในขณะที่เราสามารถนับจํานวนตัวอักษรของ String ไดจาก method length() )
แบบฝกหัด:
- การหาตําแหนงของคามากสุด/นอยสุดใน Array
- การนับความถีข
่ องตัวอักษร (อานขอมูลจากแปนพิมพ แลวแสดงความถีข
่ องแตละตัว)
- การเรียงลําดับขอมูลใน Array
- Selection sort (หาตัวมากที่สุดไปวางไวขา งหลัง/หาตัวนอยสุดไปวางไวขางหนา)
- Insertion sort (คอยๆ แทรกขอมูลไปไวตําแหนงที่เหมาะสม)
- การคนหาขอมูลใน Array
- ใช Linear (sequential) search (กรณีขอมูลไมเรียงลําดับ)
- ใช Binary Search (ขอมูลตองเรียงลําดับแลว)
© Copyright Khaitong-2-5-0-9. 2003
Page: 9 of 12
© Copyright Khaitong-2-5-0-9. 2003
Page: 10 of 12
:: INT304 ::
Programming Syntax & Semantics
Assignments:
(1)
 Matrix ADT:
Attributes:
table: 2 Dimension Array
row, column
:: INT304 ::
Programming Syntax & Semantics
Java API
 คลาส Arrays ตอง import java.util.*;
~
Arrays.binarySearch()
static int binarySearch(int[] a, int key)
static int binarySearch(double[] a, double key)
static int binarySearch(Object[] a, Object key)
~
Constructors:
Matrix()
Matrix(int row, int column)
Matrix(double[] ritem, double[] citem)
Matrix(double[][] item)
Matrix(Matrix m)
Arrays.equals()
static boolean equals(int[] a, int[] b)
static boolean equals(double[] a, double[] b)
static boolean equals(Object[] a, Object[] b)
~
Arrays.fill()
static void equals(int[] a, int val)
static void equals(int[] a, int from, int to,int val)
static void equals(Object[] a, Object val)
~
Methods:
setMatrix(int rpos, int cpos, double val) ;
setMatrix(double []ritem, double [] citem) ;
setMatrix(double [][] item) ;
matrixCopy(Matrix m) ;
add (Matrix m) ;
substract (Matrix m) ;
multiply (Matrix m) ;
equals(Matrix m);
© Copyright Khaitong-2-5-0-9. 2003
(1)
Page: 11 of 12
Arrays.sort()
static void sort(int[] a)
static void sort(int[] a, int from, int to)
static void sort(Object[] a)
Example:
String name[] = { new String(“Somchai”), new String(“Anuchart”),
new String(“Khunjaew”), new String(“The matrix”) };
int pos = Arrays.binarySearch(name, “Khunjaew”);
Arrays.sort(name);
System.out.println(name[0]);
Arrays.binarySearch(name, 1, 2, “Unknown”);
System.arraycopy(…)
© Copyright Khaitong-2-5-0-9. 2003
Page: 12 of 12
Download