Presentation is loading. Please wait.

Presentation is loading. Please wait.

Names Variables Type Checking Strong Typing Type Compatibility 1.

Similar presentations


Presentation on theme: "Names Variables Type Checking Strong Typing Type Compatibility 1."— Presentation transcript:

1 Names Variables Type Checking Strong Typing Type Compatibility 1

2 Names A name is a string of characters used to identify some entity in a program Associated with variables, labels, subprograms, formal parameters etc Names are often referred to as identifiers 2

3 Design issues for names: ◦ Maximum length? ◦ Are connector characters allowed? ◦ Are names case sensitive? ◦ Are special words reserved words or keywords? 3

4 Names (continued) Length ◦ If too short, they cannot be connotative ◦ Language examples:  FORTRAN I: maximum 6  COBOL: maximum 30  FORTRAN 90 and ANSI C: maximum 31  Ada and Java: no limit, and all are significant  C++: no limit, but implementers often impose one 4

5 Names (continued) Connectors ◦ Pascal, Modula-2, and FORTRAN 77 don't allow ◦ Others do 5

6 Names (continued) Case sensitivity ◦ Disadvantage: readability (names that look alike are different)  worse in C++ and Java because predefined names are mixed case (e.g. IndexOutOfBoundsException ) ◦ C, C++, and Java names are case sensitive  The names in other languages are not 6

7 Special 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, therefore Real is a keyword)  Real = 3.4 ( Real is a variable) ◦ A reserved word is a special word that cannot be used as a user-defined name 7

8 Variables A variable is an abstraction of a memory cell Variables can be characterized by the following attributes: ◦ Name ◦ Address ◦ Value ◦ Type ◦ Lifetime ◦ Scope 8

9 Variables Attributes Name – string of characters used to identify an entity in a program Address - the memory address with which it is associated ◦ A variable may have different addresses at different times during execution ◦ A variable may have different addresses at different places in a program ◦ If two variable names can be used to access the same memory location, they are called aliases ◦ Aliases are created via pointers, reference variables, C and C++ unions ◦ Aliases are harmful to readability (program readers must remember all of them) 9

10 Variables Attributes (continued) Type ◦ determines the range of values of variables and the set of operations that are defined for values of that type; in the case of floating point, type also determines the precision Value ◦ the contents of the location with which the variable is associated Abstract memory cell ◦ the physical cell or collection of cells associated with a variable 10

11 Type Checking Type checking is the activity of ensuring that the operands of an operator are of compatible types A compatible type is one that is either legal for the operator, or is allowed under language rules to be implicitly converted, by compiler- generated code, to a legal type ◦ This automatic conversion is called a coercion A type error is the application of an operator to an operand of an inappropriate type 11

12 Type Checking If all type bindings are static, nearly all type checking can be static If type bindings are dynamic, type checking must be dynamic 12

13 Strong Typing A programming language is strongly typed if type errors are always detected Advantage of strong typing: allows the detection of the misuses of variables that result in type errors 13

14 Strong Typing: Language examples ◦ FORTRAN 77 is not : EQUIVALENCE(An EQUIVALENCE statement is used to specify the sharing of the same storage units by two or more variables or arrays.) ◦ Pascal is not: variant records (can assign any simple type of values in a variant variable. The type of a value stored in a variant is only determined at runtime) ◦ C and C++ are not: In functions parameter type checking can be avoided; unions are not type checked ◦ Ada is, almost (UNCHECKED CONVERSION is loophole) (Unchecked Conversion copies the bytes of the source value to the target without checking whether this is sensible) (Java is similar) 14

15 Type Compatibility Name Type Compatibility Structure Type Compatibility 15

16 Name Type Compatibility Name type compatibility means the two variables have compatible types if they are in either the same declaration or in declarations that use the same type name Easy to implement but highly restrictive: ◦ Sub ranges of integer types are not compatible with integer types ◦ Formal parameters must be the same type as their corresponding actual parameters (Pascal) 16

17 Structure Type Compatibility Structure type compatibility means that two variables have compatible types if their types have identical structure More flexible, but harder to implement 17

18 Problems with Structured Type Compatibility ◦ Are two record types compatible if they are structurally the same but use different field names? ◦ Are two array types compatible if they are the same except that the subscripts are different? (e.g. [1..10] and [0..9]) ◦ Are two enumeration types compatible if their components are spelled differently? ◦ With structural type compatibility, you cannot differentiate between types of the same structure (e.g. different units of speed, both float) 18


Download ppt "Names Variables Type Checking Strong Typing Type Compatibility 1."

Similar presentations


Ads by Google