Lec2_Data_types - ITI 1121 Introduction to Computing II...

Info icon This 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.
Image of page 1

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

View Full Document Right 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.
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;
Image of page 3

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

View Full Document Right 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;
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.
Image of page 5

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

View Full Document Right 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.
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.
Image of page 7

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

View Full Document Right 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.
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 .
Image of page 9

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

View Full Document Right 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 β
Image of page 10
Class Integer In the following examples, we’ll be using our own class Integer .
Image of page 11

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern