Values, variables and types © Allan C. Milne v14.12.10.

18
Values, variables and types © Allan C. Milne v14.12.10

Transcript of Values, variables and types © Allan C. Milne v14.12.10.

Page 1: Values, variables and types © Allan C. Milne v14.12.10.

Values, variablesand types

© Allan C. Milne

v14.12.10

Page 2: Values, variables and types © Allan C. Milne v14.12.10.

Agenda.

• Values & variables.

• Types.

• Classes & objects.

• Class members.

Page 3: Values, variables and types © Allan C. Milne v14.12.10.

Introduction.

• There is often confusion between values, variables and types.

• Similarly for objects, fields and classes.• We will try to explain the concepts and

clarify the terminology.

Page 4: Values, variables and types © Allan C. Milne v14.12.10.

Values.

• Values are the bit sequences in memory that represent some quantity.

• Values might be – primitive quantities (eg integers, reals ), or– structured quantities (eg objects, structs).

• Values may be denoted in a program by– a literal constant, or– an expression.

Page 5: Values, variables and types © Allan C. Milne v14.12.10.

Values are …

• … stored in memory.

• … of some specific type of quantity.

• … the results of evaluating expressions.

• … passed as actual parameters.

• … returned by method calls.

Page 6: Values, variables and types © Allan C. Milne v14.12.10.

Variables …

• … have a name and a type;– defined in a local declaration.

• … have a location in memory;– allocated by the compiler,– contains the value of the variable,– the value is of the type of the variable.

Page 7: Values, variables and types © Allan C. Milne v14.12.10.

Variables.

• Variables are the names of memory locations that contain values.

• Fundamentally, a variable name denotes the memory location.

• However, the variable name is also used to denote its associated value.

• E.g. x = x + 1– L-Value is denotation of memory location.– R-value is the value stored in the location.

Page 8: Values, variables and types © Allan C. Milne v14.12.10.

Variables and identifiers.

• Variables are often mistakenly treated as being the same as identifiers.

• An identifier is a user-defined name.• Identifiers are used to name many

different kinds of program elements; eg– variables,– fields,– classes,– methods,– formal parameters, – etc

Page 9: Values, variables and types © Allan C. Milne v14.12.10.

Types.

• Types define the kind of values used in a program.

• User-defined types can be created.

• Types describe kinds of values, they do not have values themselves.

• Types form the basis of checking that actions are compatible or valid.

Page 10: Values, variables and types © Allan C. Milne v14.12.10.

Value types …

• … are represented by a sequence of bits in memory.– E.g. a 32-bit integer.

• … are equal if their bit sequences are identical.

int x;

x 32-bit integer

the value

x is a variable of type int

Page 11: Values, variables and types © Allan C. Milne v14.12.10.

Reference types …

• … combine the location (address or reference) of a value and its bit sequence.

• … values can be compared in two ways:– by their references, or– by the bit sequences of their values.

String s;

s address the reference

Stringobject

the object value

Page 12: Values, variables and types © Allan C. Milne v14.12.10.

A method signature.

• int Example (double d, string s) {….}– int is the return type;

– Example is the method name;

– double d, string s are the formal parameters;• note these have a type and a name.

• The method signature does not include the method body {}.

Page 13: Values, variables and types © Allan C. Milne v14.12.10.

A class is a type.• A class is a reference type.• A class defines the kind of things that must be

in values of the class.– i.e. its members.

• Defining a class does not create any value, only the class type “description”.

Page 14: Values, variables and types © Allan C. Milne v14.12.10.

An object is a value.

• An object (or instance) of a class is a value of the class type.

• As with all types, there may be many values (objects) of a class.

• Since a class is a reference type, the value of the actual object can only be accessed via a reference (pointer) to the object.

Page 15: Values, variables and types © Allan C. Milne v14.12.10.

So remember …

• The relationship and difference between values, variables and types.

• A class is a type.• An object is a value of a class type;

– also called an instance of the class.• … woe is you if you confuse classes

and objects in my hearing !!!

Page 16: Values, variables and types © Allan C. Milne v14.12.10.

Class members.• Members of a class can be

– fields, – methods,

• Each member of a class is defined in terms of – its name and type;– whether its access is private, protected or public.

• fields are created when an object is created;– i.e. the storage for a field being allocated within the storage

for the object.

Page 17: Values, variables and types © Allan C. Milne v14.12.10.

‘Variables’ revisited …

• We can have variable-like behaviour for

– local variables;

– class fields;

– formal parameters.

• While these all act similarly as locations for storing values, there are differences in terms of storage allocation, scope and lifetime.

Page 18: Values, variables and types © Allan C. Milne v14.12.10.

…. and their differences.• Local variables are

– allocated on block entry;

– removed on block exit;

– only accessible within the bloc in which they are defined.

• Class fields are – allocated on object creation;

– removed on object destruction;

– accessibility depends on its declared access privelege.

• Formal parameters are – allocated on method call;

– removed on return from the method call;

– accessible only within the method body.