Lec2_Data_types - ITI 1121. Introduction to Computing II...

Info iconThis preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon
ITI 1121. Introduction to Computing II * Marcel Turcotte School of Information Technology and Engineering Version of February 8, 2010 Abstract Data types and memory representation Scoping * These lecture notes are meant to be looked at on a computer screen. Do not print them unless it is necessary.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Today What are variables and data types; Primitive vs reference; Comparisons (primitive vs references); Auto-boxing/auto-unboxing; Passing parameters; Lexical variable scope; Memory management.
Background image of page 2
Variables What is a variable? A variable is a place in memory, to hold a value , which we refer to with help of a label; (i) 4345642 33 int i = 33;
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
In general, I will be using Greek letters to designate memory locations (addresses) because we don’t know its value, and should not care! (i) α 33 int i = 33;
Background image of page 4
Data types What are data types for? Yes, it tells the compiler how much memory to allocate: double formula; // 8 bytes char c; // 2 bytes But it also? It gives information about the meaning (semantic) of the data: which operations are allowed , which data are compatible. Hence the following statement, c = flag * formula; will produce an error at compile time; data types are therefore also useful to help detect errors in programs early on.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Data types (contd) To be more precise, there are concrete data types and abstract data types . Concrete data types specify both the allowed operations and the representation of the data. Abstract Data Types (ADTs) specify only the allowed operations.
Background image of page 6
Data Types in Java In Java, we have primitive and reference data types: Primitive are: numbers, characters (but not Strings) and booleans, – the value is stored at the memory location designated by the label of the variable ; References: Predefined: * Arrays, * Strings; User defined, reference to an instance of a class; – The value of a reference variable is a memory location which points/references to the location of an object; it’s a pointer, a “link”, its a reference ; The declaration of a reference variable does not create an object, does not allocate space for an object , it only allocates memory to store the address of an object.
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
> int a []; a = new int[5]; (a) α null The declaration of a reference variable only allocates memory to hold a reference (sometimes called pointer or address); the initial value is null , which is a special value that does not reference any object.
Background image of page 8
int a[]; > a = new int[ 5 ]; (a) α β β (instance) ± 0 0 0 0 0 The creation of a new instance, new int[ 5 ] , allocates memory to hold 5 integer values (plus some more for house keeping). Each cell of the array is initialized with the default int value, which is 0. Finally, the address of the newly created object is assigned to the location designed by the label a .
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Memory diagram Because we don’t know (and shouldn’t care) about the actual memory layout, we often use memory diagrams such as the following, (instance) β 0 0 0 0 0 a β
Background image of page 10
Class Integer In the following examples, we’ll be using our own class Integer .
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/06/2011 for the course ITI 1121 taught by Professor Samaan during the Spring '10 term at University of Ottawa.

Page1 / 56

Lec2_Data_types - ITI 1121. Introduction to Computing II...

This preview shows document pages 1 - 12. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online