Objects and Variables Local variables – Confined to single context: allocated on stack – Primitive types such as int or object references – Must be initialized.

Slides:



Advertisements
Similar presentations
1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs.
Advertisements

Chapter 6 Data Types
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.
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
CS-1030 Dr. Mark L. Hornick 1 Pointers And Dynamic Memory.
SPL/2010 Pointers and Parameter Passing in C++ 1.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
Classes and Objects: Chapter 8, Slide 1 Object and its encapsulation.
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
Intro to Java Part II. Calling an Objects Methods Use qualified names to call the objects methods. To form – you append the method name to an object reference.
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.
Session 1 CS-240 Data Structures Binghamton University Dick Steflik.
Memory and C++ Pointers.  C++ objects and memory  C++ primitive types and memory  Note: “primitive types” = int, long, float, double, char, … January.
Reference Counters Associate a counter with each heap item Whenever a heap item is created, such as by a new or malloc instruction, initialize the counter.
Peter Juszczyk CS 492/493 - ISGS. // Is this C# or Java? class TestApp { static void Main() { int counter = 0; counter++; } } The answer is C# - In C#
Storage & Linkage: Effects on Scope Rudra Dutta CSC Spring 2007, Section 001.
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
Presented by: Mojtaba Khezrian. Agenda Object Creation Object Storage More on Arrays Parameter Passing For Each VarArgs Spring 2014Sharif University of.
EECE 310: Software Engineering Lecture 2: Understanding Objects in Java and Types.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
Everything Is an Object Manipulate objects with references The identifier you manipulate is actually a “reference” to an object. Like a television.
Advanced Java Programming CS 537 – Data Structures and Algorithms.
Implications of Inheritance COMP206, Geoff Holmes and Bernhard Pfahringer.
Peyman Dodangeh Sharif University of Technology Fall 2013.
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.
C++ Memory Overview 4 major memory segments Key differences from Java
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Java Basics.  To checkout, use: svn co scb07f12/UTORid  Before starting coding always use: svn update.
More C++ Features True object initialisation
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Copyright Curt Hill Variables What are they? Why do we need them?
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Constructors & Garbage Collection Ch. 9 – Head First Java.
ISBN Object-Oriented Programming Chapter Chapter
Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Peyman Dodangeh Sharif University of Technology Spring 2014.
Object-Based Programming in VB.NET. Must Understand Following: Encapsulation Information hiding Abstract Data Type Class, Instance, Reference Variable.
Classes, Arrays & Pointers. Compiler & Linker expectations file1.cppfile2.cppfilen.cpp …. file1.ofile2.ofilen.o …. Linker application (executable) Compiler.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Sections Basic Data Structures. 1.5 Data Structures The way you view and structure the data that your programs manipulate greatly influences your.
Memory Management in Java Mr. Gerb Computer Science 4.
POINTERS AND MEMORY ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
Objects and Memory Mehdi Einali Advanced Programming in Java 1.
Data Types Chapter 6: Data Types Lectures # 13. Topics Chapter 6: Data Types 2 Introduction Primitive Data Types Character String Types Array Types Associative.
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
Design issues for Object-Oriented Languages
EECE 309: Software Engineering
C11, Implications of Inheritance
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Java Review: Reference Types
Advanced Programming Behnam Hatami Fall 2017.
Lecture 11 Memory Richard Gesick.
CLASS DEFINITION (FIELDS)
Assessment – Java Basics: Part 1
EECE 310: Software Engineering
List Allocation and Garbage Collection
String Class.
Classes and Objects Object Creation
Run-time environments
SPL – PS2 C++ Memory Handling.
CMSC 202 Constructors Version 9/10.
Presentation transcript:

Objects and Variables Local variables – Confined to single context: allocated on stack – Primitive types such as int or object references – Must be initialized before use (or fail compilation) Objects – Shared among multiple procedure contexts – Allocated on heap using new operator – Can be initialized after creation (by constructor) 1

Variables and Objects: Example int i = 6; int j; int [] a = {1, 3, 5, 7, 9}; int [] b = new int[3]; String s = “abcdef”; String t = null; j = i; b = a; t = s; Stack i = 6 j a b s t = null heap “abcdef” j = 6 t 2

Object references All object references are uninitialized initially – Can be initialized to null, but not necessary Need to explicitly allocate the object on the heap or assign it to (the start of ) an existing object – No pointer arithmetic possible once assigned – No need to explicitly de-allocate the reference (garbage collection frees it when not in use) Can be passed to procedures and copied around 3

Example of Objects and References { Object b = null; { Object a = new Object(); b = a; } c = b.hashCode(); } 4 Reference b is allocated on stack and initialized to null Reference a is allocated on stack Object is allocated on the heap and reference a points to it b and a both point to the same object a goes out of scope, so only b points to object b goes out of scope too, so nobody points to the object. Object is automatically reclaimed by garbage collector

Object Mutability By default, Java objects are mutable – Modifications made through one reference will be visible when the object is accessed through another reference to the object Example: Arrays – int [] a = {1, 3, 5, 7, 9}; – a[3] = -1; – b = a; – b[4] = -2;

Exception: Immutable objects State of immutable object never changes once it is assigned Example: String object String s1 = “abcdef”; String s2 = “ghij”; String s3 = s1; s3 = s1 + s2; String s4 = s3; s4 = s2 + s1; “abcdef” “ghij” “abcdefghij” “ghijabcdef” 6 Heap

Group Activity: Try it yourself What happens after these ? int[ ] a = {1, 2, 3}; int[ ] b = new int[2]; int[] c = a; int x = c[0]; b[0] = x; a[1] = 6; x = b[1]; int y = a[1]; What happens after these ? String s1 = “ace”; String s2 = “f”; String s3 = s1; String s4 = s3 + s2; s1 = s4; s4 = s1 + s2; 7