CS106A, Stanford University

Slides:



Advertisements
Similar presentations
 Variables  What are they?  Declaring and initializing variables  Common uses for variables  Variables you get “for free” in Processing ▪ Aka: Built-in.
Advertisements

CS 106 Introduction to Computer Science I 12 / 04 / 2006 Instructor: Michael Eckmann.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
Presented by: Mojtaba Khezrian. Agenda Object Creation Object Storage More on Arrays Parameter Passing For Each VarArgs Spring 2014Sharif University of.
Complex objects How might you design a class called NestedRects of graphical objects that look like this? Requirements for the constructor: –Like many.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n.
CS107 References and Arrays By Chris Pable Spring 2009.
CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values.
Peyman Dodangeh Sharif University of Technology Fall 2013.
Week 2. Functions: int max3(int num1, int num2, int num3) {int result; result = max(max(num1,num2),num3); return result; } //max3.
1 9/6/05CS360 Windows Programming CS360 Windows Programming.
Lab 4 - Variables. Information Hiding General Principle: – Restrict the access to variables and methods as much as possible Can label instance variables.
Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java.
Arrays  an array of 5 ints is filled with 3,2,4,1,7 int a[5] = {3,2,4,1,7}; // note squiggly brackets  an array of 3 floats is filled with 3.2,1.4,0;
© A+ Computer Science - In Java, any variable that refers to an Object is a reference variable. The variable stores the memory.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 Khalid Siddiqui.
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.
02/09/2005 Introduction to Programming with Java, for Beginners Arrays (of primitives)
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
“Success consists of going from failure to failure without loss of enthusiasm.” Winston Churchill.
3.1 Objects. Data type. Set of values and operations on those values. Primitive/Built-In types. n Usually single values n Can build arrays but they can.
April 13, 1998CS102-02Lecture 3-1 Data Types in Java CS Lecture 3-1 Java’s Central Casting.
Dynamic Storage Allocation
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Review.
CS106A, Stanford University
Yung-Hsiang Lu Purdue University
CS106A, Stanford University
CS 106A, Lecture 15 Events and Memory
Objects First with Java CITS1001
Pointers and Dynamic Variables
CISC/CMPE320 - Prof. McLeod
Advanced Programming Behnam Hatami Fall 2017.
CS 106A, Lecture 12 More Graphics
CS106A, Stanford University
CSC 253 Lecture 8.
CS106A, Stanford University
CS 106A, Lecture 22 More Classes
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
CSC 253 Lecture 8.
SSEA Computer Science: Track A
CS106A, Stanford University
Graphics.
CS106A, Stanford University
CS106A, Stanford University
CS106A, Stanford University
Dynamic Memory A whole heap of fun….
Recap Week 2 and 3.
CS106A, Stanford University
Arrays an array of 5 ints is filled with 3,2,4,1,7
CS106A, Stanford University
Introduction to Primitives
Java Programming Language
CS106A, Stanford University
CS106A, Stanford University
Java’s Central Casting
Names of variables, functions, classes
References Revisted (Ch 5)
Classes and Objects Object Creation
SPL – PS2 C++ Memory Handling.
Week 7 - Monday CS 121.
Presentation transcript:

CS106A, Stanford University References Chris Piech CS106A, Stanford University

Learning Goals Be able to write a large program Be able to trace memory with references

Today, we build!

Milestones Milestone 1 Milestone 2

Advanced memory model

Core memory model

Stack Diagrams public void run() { println(toInches(5)); } private int toInches(int feet){ int result = feet * 12; return result; run

Stack Diagrams public void run() { println(toInches(5)); } private int toInches(int feet){ int result = feet * 12; return result; run

Stack Diagrams 5 run toInches feet public void run() { println(toInches(5)); } private int toInches(int feet){ int result = feet * 12; return result; f run toInches feet 5

Stack Diagrams 5 60 run toInches feet result public void run() { println(toInches(5)); } private int toInches(int feet){ int result = feet * 12; return result; f run toInches feet 5 result 60

Stack Diagrams 5 60 run toInches feet stack result public void run() { println(toInches(5)); } private int toInches(int feet){ int result = feet * 12; return result; f run toInches feet 5 stack result 60

Stack Diagrams public void run() { println(toInches(5)); } private int toInches(int feet){ int result = feet * 12; return result; f run 60

Aside: Actual Memory

What is a bucket feet 5

What is a bucket 1011100111100011 0011010110010100 feet * Each bucket or “word” holds 64 bits

#0: don’t think on the binary level (yet)

End aside

Primitives vs Classes Primitive Variable Types Class Variable Types int double char boolean GRect GOval Gline Color Class variables (aka objects) Have upper camel case types You can call methods on them Are constructed using new Are stored in a special way

How do you share wikipedia articles? Antelope Canyon Article Key: https://en.wikipedia.org/wiki/Antelope_Canyon

Objects store addresses (which are like URLs)

What does an object store?

Objects store addresses (which are like URLs)

By Chris Piech

origin Nick Troccoli By Chris Piech

Once upon a time…

…a variable x was born! int x;

…a variable x was born! int x;

x was a primitive variable… int x; It’s so cuuuute! Aww…!

…and its parents loved it very much. int x; We should give it…. value 27!

…and its parents loved it very much. x = 27; We should give it…. value 27! 27

A few years later, the parents decided to have another variable.

…and a variable rect was born! GRect rect;

rect was an object variable… GRect rect; Who’s a cute GRect??? It’s so square!

…and its parents loved it very much. GRect rect; We should make it…. a big, strong GRect!

…and its parents loved it very much. GRect rect = new Grect(0, 0, 50, 50); We should make it…. a big, strong GRect!

…but rect’s box was not big enough for an object! GRect rect = new Grect(0, 0, 50, 50); That box isn’t big enough to store everything about a GRect!

…so they stored the information in a bigger box somewhere else. GRect rect = new Grect(0, 0, 50, 50); x = 0, y = 0 width = 50 height = 50 ... Stack and heap Location 5 See location 5

Chapter 2: Coming soon

public void run() { GRect r = null; } Method memory Object memory

public void run() { GRect r = null; } Method memory Object memory run r null

Wahoo!

public void run() { GRect r = new GRect(50, 50); } Method memory Object memory run r

public void run() { GRect r = new GRect(50, 50); } Method memory Object memory memory.com/18 run r

public void run() { GRect r = new GRect(50, 50); } Method memory Object memory memory.com/18 run r memory.com/18

public void run() { GRect r = new GRect(50, 50); } Method memory Object memory run 18 r 18

public void run() { GRect r = new GRect(50, 50); } Method memory Object memory run r

public void run() { GRect r = new GRect(50, 50); r. setColor(Color public void run() { GRect r = new GRect(50, 50); r.setColor(Color.BLUE); r.setFilled(true); } Method memory Object memory run r

public void run() { GRect r = new GRect(50, 50); r. setColor(Color public void run() { GRect r = new GRect(50, 50); r.setColor(Color.BLUE); r.setFilled(true); } Method memory Object memory run r

public void run() { GRect r = new GRect(50, 50); r. setColor(Color public void run() { GRect r = new GRect(50, 50); r.setColor(Color.BLUE); r.setFilled(true); } Method memory Object memory run r

#1: new allocates memory for objects * The data for an object can’t always fit inside a fixed size bucket

#2: object variables store addresses #ultimatekey

What does an object store?

Objects store addresses (which are like URLs)

Memory Object Memory Instance Variables www.memory.com/12 canvas run www.memory.com/12 r

Memory Object Memory Instance Variables www.memory.com/12 canvas run www.memory.com/12 r

Memory Object Memory Instance Variables 12 canvas 12 run 12 r

Memory Heap Instance Variables 12 canvas 94 mousePressed x = 72 94 e time = 192332123 e 12 obj

What does an object store?

Objects store addresses (which are like URLs)

Finish Up

Learning Goals Be able to write a large program Be able to trace memory with references