Object References. Objects An array is a collection of values, all of the same type An object is a collection of values, which may be of different types.

Slides:



Advertisements
Similar presentations
Computer Programming Lecture 14 C/C++ Pointers
Advertisements

25-Aug-14 Pointers and References. Machine addresses Computer memory consists of one long list of addressable bytes A pointer is a data item that contains.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
CIT 590 Intro to Programming Java lecture 3. Hashmaps The equivalent of python dictionaries. With both ArrayLists and Hashmaps, the syntax only allows.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
Pointer. Warning! Dangerous Curves C (and C++) have just about the most powerful, flexible and dangerous pointers in the world. –Most other languages.
18-Jun-15 Arrays. 2 A problem with simple variables One variable holds one value The value may change over time, but at any given time, a variable holds.
20-Jun-15 Methods. Complexity The programmer's biggest adversary is complexity The programmer's biggest adversary is complexity Primitive types and the.
22-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.
26-Jun-15 Arrays. 2 A problem with simple variables One variable holds one value The value may change over time, but at any given time, a variable holds.
CS 61C L03 C Arrays (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #3: C Pointers & Arrays
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
Adapted from Dr. Craig Chase, The University of Texas at Austin.
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
C Arrays and Pointers In Java, pointers are easy to deal with –In fact, there is little that can go wrong in Java since pointer access is done for you.
Week 10 - Monday.  What did we talk about last time?  Method overloading  Lab 9.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
CSE 232: C++ pointers, arrays, and references Overview of References and Pointers Often need to refer to another object –Without making a copy of the object.
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
Pointers. The structure of memory Computer memory is a linear sequence of addressable locations Addresses are numbered starting at zero In personal computers,
SPL – Practical Session 2 Topics: – C++ Memory Management – Pointers.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
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.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Pointers *, &, array similarities, functions, sizeof.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
24-Dec-15 Class Structure. 2 Classes A class describes a set of objects The objects are called instances of the class A class describes: Fields (instance.
By Mr. Muhammad Pervez Akhtar
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
CSC 142 F 1 CSC 142 References and Primitives. CSC 142 F 2 Review: references and primitives  Reference: the name of an object. The type of the object.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
CS162 - Topic #12 Lecture: –Arrays with Structured Elements defining and using arrays of arrays remember pointer arithmetic Programming Project –Any questions?
Chapter 16 Pointers and Arrays Pointers and Arrays We've seen examples of both of these in our LC-3 programs; now we'll see them in C. Pointer Address.
Array contiguous memory locations that have the same name and type. Note: an array may contain primitive data BUT an array is a data structure a collection.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
User-Written Functions
Some Eclipse shortcuts
Introduction to Computer Science / Procedural – 67130
Variables and Primative Types
Pointers and References
Objects First with Java CITS1001
Pointers and References
Class Structure 16-Nov-18.
Object Oriented Programming COP3330 / CGS5409
Lecture 18 Arrays and Pointer Arithmetic
Class Structure 7-Dec-18.
Pointers and References
Class Structure 2-Jan-19.
Java Classes Aliases & Null & This
Class Structure 25-Feb-19.
C++ Pointers and Strings
Introduction to Primitives
Introduction to Primitives
Java Programming Language
Methods 16-May-19.
C++ Pointers and Strings
Classes and Methods 15-Aug-19.
Presentation transcript:

Object References

Objects An array is a collection of values, all of the same type An object is a collection of values, which may be of different types –Actually, there’s more to an object than just values, but that’s all we need to consider right now Example: class Person { String name; boolean male; int age; int phoneNumber; } Notice that different kinds of objects typically take up different amounts of space in the computer

What this talk is about We’re going to discuss how objects are stored in computer memory You might think this doesn’t matter to you, because Java just automatically does all the work of finding a place for objects and putting them there –You would be wrong A lot of things involved in creating and manipulating objects are just plain mysterious unless you know what is going on “under the hood”

Computer memory organization A bit is a single on/off, yes/no, 0 / 1 value A byte is eight consecutive bits Computer memory is a numbered sequence of bytes, starting at zero –It is impossible for a memory location to be “empty”--bits are always either on or off The number associated with a byte is called its address A 000B Addresses are binary numbers (usually written as hexadecimal), but this isn’t really relevant to the discussion Larger computer systems might address words rather than bytes, where a word is some larger number of bits (such as 32 or 64)

Primitives in memory When you declare a primitive variable, you allocate space in memory for it A byte requires one byte of memory Hence, every variable has an address Address 0000 is never used A 000B char c; byte b; int i; An int requires four consecutive bytes of memory A char requires two consecutive bytes of memory

Objects in memory When you declare an object variable, Java does not know how much space the object needs--so it can’t allocate space for it It doesn’t know how much space is needed until you assign an object to the variable Instead, what Java does is allocate space for a pointer to the object –A pointer is a variable that holds an address –A pointer requires four consecutive bytes –The pointer is initially null ( 0000 ) Later, when you assign an actual object to the variable, what Java does is to make the pointer “point to” (hold the address of) the memory occupied by the object A 000B ?

Declaring and creating objects Person p = new Person("John"); Person p allocates space to hold a reference (pointer) to a Person p: new Person("John") allocates space for the actual Person, and initializes it "John"07A3 The assignment makes p refer to the new Person 07A3

Pointers and references Java uses references rather than pointers Both mean: A variable that holds an address –You can follow or dereference either one to get to the thing that it points to –You can assign either to a variable of the correct type But you can do more with pointers: –You can treat a pointer as an integer and do arithmetic to get a new pointer –Pointers can point to anything; references refer to known objects Consequences: –Pointers are much more flexible –References allow the computer to do more for you –90% of the serious errors in C and C++ programs are pointer errors

Consequences I: Assignment When you assign to a primitive variable, you get a copy of the value Hence, two object variables can refer to the same object –You can think of them as aliases of each other –If you change the object pointed to by one of them, you are changing the same object pointed to by the other 24 x 3 y y = x; 24 b a John Smith true b = a; a.age++; 24 When you assign to an object variable, you get a copy of the reference

Consequences II: parameters We call a method like this: result = add( 3, 5 ) ; Here’s the method: int add ( int number1, int number2 ) { int sum = number1 + number2 ; return sum ; } Actual parameter values are copied into formal parameters Formal parameters are used in method A result is returned

Primitive parameters are copied in int m = 3; int n = 5; result = add( m, n ) ; int add ( int number1, int number2 ) { while (number1 > 0) { number1--; number2++; } return number2 ; } The values of m and n (that is, 3 and 5) are copied to the formal parameters Changing number1 and number2 does not affect m and n This is call by value The value of number2 is returned, but the variable number2 is thrown away

Object references are copied in Person p = new Person("John"); changeName( p ); void changeName(Person per ) { per.name = "Jack"; } This means that p and per refer to the same object! This is call by reference p holds a reference to a Person "John" It is the reference that is copied in, not the Person p is not changed, but changes made to the object referenced by per remain changed when the method returns "Jack" per.name = "Jack";

Consequences III: final When you declare a variable to be final, you cannot change its value: final int speedLimit = 65; speedLimit = 70; // illegal! The value of an object variable is the address (reference) it contains, not the object itself: final Person president = new Person("George Bush"); president = new Person("Al Gore"); // illegal president.age = president.age + 1; // legal! president.name = "Al Gore"; // legal!

Consequences IV: NullPointerException When you declare an object variable, but before you assign it a value, it has the value null ( 0000 ) null is a legal value for any object variable –You can test if a variable has value null –You can assign null like any other value –You can not use the referenced object, because there isn’t any! Example: Person clerk; clerk.name = "John Smith"; // NullPointerException

Consequences V: Equality When you test two primitives for equality, you are testing their values Hence, a == b, but a != c For Strings you can use the equals method, s1.equals(s2), but for other kinds of objects it isn’t so simple 24 x y When you test two objects for equality, you are testing their references b a John Smith true c John Smith true

Arrays Arrays are objects! int[ ] scores; scores[0] = 76; // NullPointerException int[ ] testResults = new int[125]; testResults[0] = 98; int[ ] results = testResults; System.out.println(results[0]); // prints 98 int[ ] numbers = new int[10]; System.out.println(numbers[5]); // prints 0 (why?) Person[ ] people = new Person[10]; System.out.println(people[5]); // prints null (why?) System.out.println(people[5].name); // NullPointerException

The End