Object References James Brucker.

Slides:



Advertisements
Similar presentations
Dynamic memory allocation
Advertisements

Primitives, References and Wrappers Java has the following types defined as part of the java language; int float double byte char boolean long short These.
Chapter 6 Data Types
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Analysis of programs with pointers. Simple example What are the dependences in this program? Problem: just looking at variable names will not give you.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding.
Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Objectives Learn about objects and reference variables Explore how to use predefined methods in a program.
Introduction to Programming with Java, for Beginners
CSE 2501 Review Declaring a variable allocates space for the type of datum it is to store int x; // allocates space for an int int *px; // allocates space.
Arrays Horstmann, Chapter 8. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
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.
Introduction to Computers and Programming Strings Professor: Evan Korth New York University.
Week 10 - Monday.  What did we talk about last time?  Method overloading  Lab 9.
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
SE-1010 Dr. Mark L. Hornick 1 Defining Your Own Classes Part 3.
Data Objects (revisited) Recall that values are stored in data objects, and that each data object holds one value of a particular type. Data objects may.
Chapter 7 Objects and Memory. Structure of memory The fundamental unit of memory is called a bit, either 0 or 1. In most modern architectures, the smallest.
VARIABLES AND TYPES CITS1001. Types in Java the eight primitive types the unlimited number of object types Values and References The Golden Rule Scope.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
1 JAVA & MEMORY What did I just say this topic was about ?
Chapter 8: Arrays.
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
Advanced Java Programming CS 537 – Data Structures and Algorithms.
SPL – Practical Session 2 Topics: – C++ Memory Management – Pointers.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
CSE 501N Fall ‘09 04: Introduction to Objects 08 September 2009 Nick Leidenfrost.
ACO 101: Intro to Computer Science Anatomy Part 3: The Constructor.
Sahar Mosleh California State University San MarcosPage 1 The Class String There is no primitive type for strings in Java The class String is a predefined.
Coding Bat: Ends in ly Given a string of even length, return a string made of the middle two chars, so the string "string" yields "ri". The string.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Objects and Variables Local variables – Confined to single context: allocated on stack – Primitive types such as int or object references – Must be initialized.
Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java.
Programming Languages and Paradigms Activation Records in Java.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
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.
Week 10 - Monday.  What did we talk about last time?  Method overloading  Lab 9.
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.
02/09/2005 Introduction to Programming with Java, for Beginners Arrays (of primitives)
Designing Classes Lab. The object that you brought to class Put it in the basket we will exchange them now.
Sections Basic Data Structures. 1.5 Data Structures The way you view and structure the data that your programs manipulate greatly influences your.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
2-Oct-16 Basic Object-Oriented Concepts. 2 Concept: An object has behaviors In old style programming, you had: data, which was completely passive functions,
Chapter 2 Basic Computation
Java Review: Reference Types
Chapter 2 Basic Computation
Basic notes on pointers in C
An Introduction to Java – Part II
String Methods: length substring
Lecture 11 Memory Richard Gesick.
Memory Allocation CS 217.
Introduction to Objects
Arrays.
CS2011 Introduction to Programming I Objects and Classes
Dynamic Memory A whole heap of fun….
RDBMS Chapter 4.
Assessment – Java Basics: Part 1
Object Oriented Programming Review
Dynamic Memory A whole heap of fun….
List Allocation and Garbage Collection
Web Design & Development Lecture 4
Dr. Sampath Jayarathna Cal Poly Pomona
String Class.
Variables and Constants
SPL – PS2 C++ Memory Handling.
CSE 303 Concepts and Tools for Software Development
Presentation transcript:

Object References James Brucker

Example Person a = new Person("Ant"); Person b = new Person("Nok"); System.out.println(a); // "Ant" b = a; a.setName("Bat"); System.out.println(a); // "Bat" // what is printed? System.out.println(b); Person -name: String Person(name: String) getName(): String setName(name: String) toString(): String UML Class Diagram shows the attributes and methods of a class.

This is important -- know it! Variables A variable is a name we use to refer to a memory location. What is stored in the memory location? Program: Memory: /* define two variables */ int x; int y; String s; x y s We will see that the answer is different for variables of primitive data types and variables of object data types. This is important -- know it!

Primitive Data Types are stored as values For primitive data types, the variable's memory location holds its value. Data types for which this is true are called value data types. Program: Memory: /* define two "int" variables */ int x; int y; x y 4 bytes x 25 /* assign value to x */ x = 25; y x 25 /* assign value to y */ y = x; y 25

Values are copied on assignment Changing the value of x does not affect the value of y. x 100 x = 100; y 25 x 100 /* assign value to y */ y = 40; y 40

Variables for Object Data Types For object data types, a variable is a reference to the object, but does not store the object !! Program: Memory: /* define two String variables */ String s; String t; s t a few bytes

Variables refer to the Object's location To create an object you must use the "new" keyword. This allocates new storage in a memory region called the heap. s 01008 String s; /* create an object */ s = new String("Help, I'm trapped in a Computer."); t new String Address: Memory: 01000 01008 01010 01018 01020 01028 01030 01038 48AC00FB Help, I' m trappe d in a Computer .0000000 00000000 The new command creates a new object and returns a reference to its memory location The object also contains other information, such as: - length of string - the Class it belongs to

Variables refer to an Object's location Each new object gets its own storage space on the heap. The size of the object can be anything. s 01008 /* create an object */ t = new String("Hello"); t 01030 Address: Memory: 01000 01008 01010 01018 01020 01028 01030 01038 48AC00FB Help, I' m trappe d in a Computer .0000000 Hello000 The new command finds some more free "heap" space large enough for the String "Hello". It creates a String object and returns a reference to it. new String

Variables for Object Data Types (4) When you assign a value to a reference variable, you are assigning the address of the object -- not the object's value! s 01030 // copy object reference s = t; t 01030 Address: Memory: 01000 01008 01010 01018 01020 01028 01030 01038 48AC00FB Help, I' m trappe d in a Computer .0000000 Hello000 Now s refers to the same object as t ("Hello"). The old String object has no reference ... it is garbage. Eventually Java will reclaim the storage space for re-use.

The null value Now s does not refer to anything. If an object variable (object reference) doesn't refer to any object, it is assigned a value of null. You can use this to "clear" a reference. s 00000 /* discard old value */ s = null; t 01030 Address: Memory: 01000 01008 01010 01018 01020 01028 01030 01038 48AC00FB Help, I' m trappe d in a Computer .0000000 Hello000 Now s does not refer to anything. But, the old value may still be in memory (in the heap).

Another Example: BankAccount (1) BankAccount a; BankAccount b; This creates BankAccount references, but doesn't create any BankAccount objects. Memory: a null b null

BankAccount (2) a = new BankAccount( "George Bush", 11111); a.deposit(200000); :BankAccount owner="George Bush" accountID=11111 balance=200000 Memory: a b null UML Object Diagram notation, show the values of one object..

Create another BankAccount b = new BankAccount( "Taksin Shinawat", 12345); b.deposit(1000000000); :BankAccount owner="George Bush" accountID=11111 balance=200000 Memory: a b :BankAccount owner="Taksin Shinawat" accountID=12345 balance=1000000000

assign a: copy or reference? // copy Taksin's data into the other object? a = b; No copy! It makes a "point to" the same object as b. :BankAccount owner="George Bush" accountID=11111 balance=200000 Memory: a b :BankAccount owner="Taksin Shinawat" accountID=12345 balance=1000000000

Who's Got the Money? // copy Taksin's data into the other object? a = b; a.setOwner("Donald Trump"); :BankAccount owner="George Bush" accountID=11111 balance=200000 Memory: a b :BankAccount owner="Donald Trump" accountID=12345 balance=1000000000