Chapter 9 Life & Death of an Object Stacks & Heaps; Constructors Garbage Collector (gc)

Slides:



Advertisements
Similar presentations
Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
Advertisements

CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Local Variables and Scope Benjamin Fein. Variable Scope A variable’s scope consists of all code blocks in which it is visible. A variable is considered.
SPL/2010 Pointers and Parameter Passing in C++ 1.
Memory Management & Method Calls in Java Program Execution © Allan C. Milne v
Variable types We have already encountered the idea of a variable type. It is also possible to think about variables in terms of their kind, namely: 1)
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
1 Composition A whole-part relationship (e.g. Dog-Tail) Whole and part objects have same lifetime –Whole creates instance of part in its constructor In.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Run-Time Storage Organization
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.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Pointers and Dynamic Variables. Objectives on completion of this topic, students should be able to: Correctly allocate data dynamically * Use the new.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Week 5 Recap CSE 115 Spring Composition Informally called “has a” Represented in UML with a diamond- headed arc In code: Declare an instance variable.
30-Jun-15 Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything.
©2004 Brooks/Cole Chapter 10 More on Classes. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Object assignment With primitive types, setting.
1 Memory Model of A Program, Methods Overview l Memory Model of JVM »Method Area »Heap »Stack.
1 Contents. 2 Run-Time Storage Organization 3 Static Allocation In many early languages, notably assembly and FORTRAN, all storage allocation is static.
Introduction to Java Programming, 4E Y. Daniel Liang.
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
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.
1 CSCE3193: Programming Paradigms Nilanjan Banerjee Programming Paradigms University of Arkansas Fayetteville, AR
Memory organization - storing variables efficiently in the RAM memory.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
1 Chapter 3 and 6– Classes, Objects and Methods Object-Oriented Programming Concepts – Read it from Java TutorialJava Tutorial Definition: A class is a.
Initialization & Cleanup Guaranteed initialization with the constructor The name of the constructor is the same as the name of the class.
Local Variables Garbage collection. © 2006 Pearson EducationParameters2 of 10 Using Accessors and Mutators Together What if you want to get a property.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
ECE122 Feb. 22, Any question on Vehicle sample code?
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C H A P T E R F I V E Memory Management.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
Methods: A Deeper Look. Template for Class Definition public class { } A.Import Statement B.Class Comments C.Class Name D.Data members E.Methods (inc.
 Constructor  Finalize() method  this keyword  Method Overloading  Constructor Overloading  Object As an Argument  Returning Objects.
Classes: user-defined types. Organizing method with a class A class is used to organize methods * Methods that compute Mathematical functions * The Scanner.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Constructors & Garbage Collection Ch. 9 – Head First Java.
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.
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
Programming Languages and Paradigms Activation Records in Java.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Bank Account Example public class BankAccount { private double balance; public static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++;
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Lecture 11 Instructor: Craig Duckett Instance Variables.
Sections Basic Data Structures. 1.5 Data Structures The way you view and structure the data that your programs manipulate greatly influences your.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Storage Classes There are three places in memory where data may be placed: In Data section declared with .data in assembly language in C - Static) On the.
Java Memory Management
Class Inheritance Part I
Java Memory Management
Unit-2 Objects and Classes
Chapter 3: Using Methods, Classes, and Objects
Using local variable without initialization is an error.
More Object Oriented Programming
CSC 253 Lecture 8.
CSC 253 Lecture 8.
Stacks.
Which best describes the relationship between classes and objects?
Object Oriented Programming in java
Stacks.
Corresponds with Chapter 5
Presentation transcript:

Chapter 9 Life & Death of an Object Stacks & Heaps; Constructors Garbage Collector (gc)

The Stack and the Heap – where things Live Crucial to understanding what happens when creating an object Need to learn about where everything lives, and for how long, in Java The Stack and the Heap The Heap – where objects live The stack – where method invocations and local variables live.

Objects and Variables All objects lives on the garbage-collectible heap Where do variables live? – Depends on what type of variable it is – Instance Variables & local variables Local variables known as stack variable. Methods that are called and local variables live on the stack.

Instance v. Local – page 236 Instance – declared inside a class, not inside a method – Represent fields that each individual object has Local Variables – declared inside a method – Temporary; live only as long as the method is on the stack; as long as method hasn’t reached the closing curly brace

The Stack Methods are Stacked – When you call a method, it lands on the top of a call stack. – The new thing is pushed onto the stack frame, which holds the state of the method including the line of code that is executing, and any LOCAL variables. – Method at the TOP of the stack is always the currently-running method for that stack.

Stack Scenario Code and illustration on page 237 Look through each line of code Methods are called and put on the stack

What about local variables that are Objects? Remember, a non-primitive variable holds a reference to an object, not the object itself. Objects live on the heap If the local variable is a reference to an object, only the variable (reference /remote control) goes on the stack. Example – page 238

Bullet points Java has 2 areas of memory – stack & heap Instance variable declared inside a class, outside of a method Local variables declared inside a method or method parameter All local variables live on the stack, in the frame corresponding to the method declared Object reference variables work like primitive variables All objects live in the heap

Local v. Instance Variables If local variables live on the stack, where do instance variables live? When creating an object, how much space is needed for an object and the instance variables? Values of an object’s Instance variables live inside the object. Java makes space. What if instance variable is an object? Java makes the space when instance variable is assigned a value.