Values, variables and types © Allan C. Milne v14.12.10.
-
Upload
derick-merritt -
Category
Documents
-
view
213 -
download
1
Transcript of Values, variables and types © Allan C. Milne v14.12.10.
![Page 1: Values, variables and types © Allan C. Milne v14.12.10.](https://reader035.fdocuments.us/reader035/viewer/2022072014/56649e7b5503460f94b7bb74/html5/thumbnails/1.jpg)
Values, variablesand types
© Allan C. Milne
v14.12.10
![Page 2: Values, variables and types © Allan C. Milne v14.12.10.](https://reader035.fdocuments.us/reader035/viewer/2022072014/56649e7b5503460f94b7bb74/html5/thumbnails/2.jpg)
Agenda.
• Values & variables.
• Types.
• Classes & objects.
• Class members.
![Page 3: Values, variables and types © Allan C. Milne v14.12.10.](https://reader035.fdocuments.us/reader035/viewer/2022072014/56649e7b5503460f94b7bb74/html5/thumbnails/3.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022072014/56649e7b5503460f94b7bb74/html5/thumbnails/4.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022072014/56649e7b5503460f94b7bb74/html5/thumbnails/5.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022072014/56649e7b5503460f94b7bb74/html5/thumbnails/6.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022072014/56649e7b5503460f94b7bb74/html5/thumbnails/7.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022072014/56649e7b5503460f94b7bb74/html5/thumbnails/8.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022072014/56649e7b5503460f94b7bb74/html5/thumbnails/9.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022072014/56649e7b5503460f94b7bb74/html5/thumbnails/10.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022072014/56649e7b5503460f94b7bb74/html5/thumbnails/11.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022072014/56649e7b5503460f94b7bb74/html5/thumbnails/12.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022072014/56649e7b5503460f94b7bb74/html5/thumbnails/13.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022072014/56649e7b5503460f94b7bb74/html5/thumbnails/14.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022072014/56649e7b5503460f94b7bb74/html5/thumbnails/15.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022072014/56649e7b5503460f94b7bb74/html5/thumbnails/16.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022072014/56649e7b5503460f94b7bb74/html5/thumbnails/17.jpg)
‘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.](https://reader035.fdocuments.us/reader035/viewer/2022072014/56649e7b5503460f94b7bb74/html5/thumbnails/18.jpg)
…. 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.