0% found this document useful (0 votes)
33 views

Names Variables Type Checking Strong Typing Type Compatibility

This document discusses key concepts related to variables in programming languages. It describes how variables are named using identifiers and have attributes like type, value, and scope. It also discusses type checking and strong typing, explaining that strongly typed languages always detect type errors. Strong typing can be weakened by coercion rules that allow implicit type conversions. The document contrasts name type compatibility, which is more restrictive, versus structure type compatibility, which is more flexible but harder to implement.

Uploaded by

Lulu Tojeen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Names Variables Type Checking Strong Typing Type Compatibility

This document discusses key concepts related to variables in programming languages. It describes how variables are named using identifiers and have attributes like type, value, and scope. It also discusses type checking and strong typing, explaining that strongly typed languages always detect type errors. Strong typing can be weakened by coercion rules that allow implicit type conversions. The document contrasts name type compatibility, which is more restrictive, versus structure type compatibility, which is more flexible but harder to implement.

Uploaded by

Lulu Tojeen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 18

Names

Variables
Type Checking
Strong Typing
Type Compatibility

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

Design issues for names:


Maximum length?
Are connector characters allowed?
Are names case sensitive?
Are special words reserved words or
keywords?

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

Names (continued)
Connectors

Pascal, Modula-2, and FORTRAN 77


don't allow
Others do

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

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 userdefined name
7

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

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

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

Type Checking
Generalize

the concept of operands and


operators to include subprograms and
assignments
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 compilergenerated 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

If

all type bindings are static,


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

12

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

Language examples:
FORTRAN 77 is not : EQUIVALENCE
Pascal is not: variant records
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)
(Java is similar)
13

Strong Typing
Coercion

rules strongly affect


strong typing--they can weaken it
considerably (C++ versus Ada)

Although

Java has just half the


assignment coercions of C++, its
strong typing is still far less
effective than that of Ada
14

Type Compatibility
Name

Type Compatibility

Structure

Type Compatibility

15

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

Structure Type
Compatibility
Structure

type compatibility
means that two variables have
compatible types if their types
have identical structures
More flexible, but harder to
implement

17

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

You might also like