Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Values, variables and types © Allan C. Milne v14.12.10."— Presentation transcript:

1

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

3 Agenda. Values & variables. Types. Classes & objects. Class members.

4 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.

5 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.

6 Values are … … stored in memory. … of some specific type of quantity. … the results of evaluating expressions. … passed as actual parameters. … returned by method calls.

7 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.

8 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.

9 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

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.

11 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

12 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 String object the object value

13 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 {}.

14 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”.

15 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.

16 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 !!!

17 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.

18 ‘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.

19 …. 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.


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

Similar presentations


Ads by Google