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

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

Programming Languages and Paradigms The C Programming Language.
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
Object Oriented Programming COP3330 / CGS5409.  C++ Automatics ◦ Copy constructor () ◦ Assignment operator =  Shallow copy vs. Deep copy  DMA Review.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Subprogram Control - Data sharing Mechanisms to exchange data Arguments - data objects sent to a subprogram to be processed. Obtained through  parameters.
Chapter 7: User-Defined Functions II
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Methods. int month; int year class Month Defining Classes A class contains data declarations (static and instance variables) and method declarations (behaviors)
Memory Management & Method Calls in Java Program Execution © Allan C. Milne v
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
Defining classes and methods Recitation – 09/(25,26)/2008 CS 180 Department of Computer Science, Purdue University.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Run-Time Storage Organization
Understanding class definitions Looking inside classes.
String Escape Sequences
MT311 Java Application Programming and Programming Languages Li Tak Sing ( 李德成 )
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Stack and Heap Memory Stack resident variables include:
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Basic Semantics Associating meaning with language entities.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Week 6: Functions - Part 2 BJ Furman 01OCT2012. The Plan for Today Comments on midterm exam (next week in lab!) Review of functions Scope of identifiers.
Data Types Declarations Expressions Data storage C++ Basics.
Pointers *, &, array similarities, functions, sizeof.
GoodOO Programming Practice in Java © Allan C. Milne v
CSI 3125, Preliminaries, page 1 Data Type, Variables.
1 MORE ON MODULAR DESIGN: MODULE COMMUNICATIONS. 2 WHEN A FUNCTION IS INVOKED, MEMORY IS ALLOCATED LOCALLY FOR THE FORMAL PARAMETERS AND THE VALUE OF.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
CPS120: Introduction to Computer Science Data Structures.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Week 12 Methods for passing actual parameters to formal parameters.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Sadegh Aliakbary Sharif University of Technology Fall 2010.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Operational Semantics (Slides mainly.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Fundamentals 2.
Chapter 7: User-Defined Functions II
CSE 374 Programming Concepts & Tools
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Programming Languages and Paradigms
Programming Paradigms
Pointers and References
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Lecture 18 Arrays and Pointer Arithmetic
Overloading functions
Java Programming Language
C Programming Pointers
A simple function.
Programming Languages and Paradigms
Corresponds with Chapter 5
SPL – PS2 C++ Memory Handling.
Introduction to Pointers
Presentation transcript:

Values, variables and types © Allan C. Milne v

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

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.

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.

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

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.

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.

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

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.

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

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

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

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

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.

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

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.

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

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