Peyman Dodangeh Sharif University of Technology Fall 2013.

Slides:



Advertisements
Similar presentations
Programming and Data Structure
Advertisements

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.
1 COSC2767: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
CSCI 1100/ , 6.2, 6.4 April 12, 15, 17.
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.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Road Map Introduction to object oriented programming. Classes
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Memory and C++ Pointers.  C++ objects and memory  C++ primitive types and memory  Note: “primitive types” = int, long, float, double, char, … January.
Sanjay Goel, School of Business, University at Albany, SUNY 1 MSI 692: Special Topics in Information Technology Lecture 4: Strings & Arrays Sanjay Goel.
Pointers CSE 2451 Rong Shi.
OOP Languages: Java vs C++
Sadegh Aliakbary Sharif University of Technology Fall 2010.
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Presented by: Mojtaba Khezrian. Agenda Object Creation Object Storage More on Arrays Parameter Passing For Each VarArgs Spring 2014Sharif University of.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
Chapter 2: Everything is an Object ● C++ has many non object oriented features inherited from C. It is a hybrid language meaning that it support different.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Stack and Heap Memory Stack resident variables include:
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
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.
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
Everything Is an Object Manipulate objects with references The identifier you manipulate is actually a “reference” to an object. Like a television.
Questions? Suggestions?. References References Revisited What happens when we say: int x; double y; char c; ???
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.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
CPS4200 Unix Systems Programming Chapter 2. Programs, Processes and Threads A program is a prepared sequence of instructions to accomplish a defined task.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Copyright Curt Hill Variables What are they? Why do we need them?
Everything is an object (CH-2) Manipulating Objects with References. Manipulating Objects with References. String s; String s = “IS2550” String s = new.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
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.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Peyman Dodangeh Sharif University of Technology Spring 2014.
CS 180 Recitation 7 Arrays. Used to store similar values or objects. An array is an indexed collection of data values of the same type. Arrays are the.
The const Keyword Extreme Encapsulation. Humble Beginnings There are often cases in coding where it is helpful to use a const variable in a method or.
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.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Grouping Data Together Often we want to group together a number of values or objects to be treated in the same way e.g. names of students in a tutorial.
Objects and Memory Mehdi Einali Advanced Programming in Java 1.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Chapter 8 Arrays and the ArrayList Class Arrays of Objects.
Computer Organization and Design Pointers, Arrays and Strings in C
Java Memory Management
Java Memory Management
objects CIS 421 Kutztown University
Java Review: Reference Types
Advanced Programming Behnam Hatami Fall 2017.
CSC 253 Lecture 8.
CSC 253 Lecture 8.
اشیاء در جاوا Java Objects
Memory Allocation CS 217.
CS2011 Introduction to Programming I Arrays (II)
Java Programming Language
Chapter 9: Pointers and String
SPL – PS2 C++ Memory Handling.
CMSC 202 Constructors Version 9/10.
Presentation transcript:

Peyman Dodangeh Sharif University of Technology Fall 2013

Agenda Object Creation More on Arrays For Each VarArgs Object Storage Parameter Passing Sharif University of Technology2

public class Dog { private String name; public void setName(String n) { name = n; } public void bark(){ System.out.println("Hop! Hop!"); } } Dog d = new Dog();  Object Creation (instantiation) d.setName("Fido");  changing the object’s state d.bark();  passing message to object d is an object d is a reference to an object Sharif University of Technology3 Class Declaration

Object Memory Remember : an object has state, behavior and identity Each object is stored in memory Memory address ≈ object identity Memory content  object state The behavior of an object is declared in its class Class declaration is also stored in memory But class declaration is stored once for each class For each object a separate piece of memory is needed To store its state Sharif University of Technology4

new Operator new creates a new object from specified type new String(); new Book(); new int(); Primitive types are not referenced Sharif University of Technology5

new new operator creates a new object from the specified type Returns the reference to the created object String s = new String(); Dog d = new Dog(); Rectangle rectangle = new Rectangle(); Sharif University of Technology6

Object References Remember C++ pointers When you declare an object, you declare its reference String s; Book b; Exception: ? Primitive types Primitive types are not actually objects They can not have references Java references are different from C++ pointers Java references are different from C++ references Sharif University of Technology7

Create Objects This code will not create an object: String str; It just creates a reference This is a key difference between Java and C++ You can not use “str” variable “str” is null null value in java You should connect references to real objects How to create objects? new Sharif University of Technology8

new new creates a piece of memory Returns its reference Where is the piece of memory? In Heap Where is the Heap? Later… Sharif University of Technology9

Array in java Array elements are stored in heap Integer[] inumbers; Person[] people = new Person[5]; int N = … float[] realNumbers = new float[N]; Array elements are references not objects Exception : primitives Sharif University of Technology10

Primitive-Type Array Sample Sharif University of Technology11

Array Samples Sharif University of Technology12

Array References There is three type of variable in this code array reference array[i] references Initial value: null array[i] objects Sharif University of Technology13

public class Student { private String name; private Long id; public String getName() { return name; } public void setName(String name) { this.name = name; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } Sharif University of Technology14

Array Sample Student[] students = new Student[10]; for (int i = 0; i < students.length; i++) { students[i] = new Student(); students[i].setId(i); } Sharif University of Technology15

What Does Happen to Students After f() Method Invocation? void f() { Student[] students = new Student[10]; for (int i = 0; i < students.length; i++) { students[i] = new Student(); students[i].setId(i); } void g() { f(); } Sharif University of Technology16

For Each Sharif University of Technology17

For Each (2) In for each expression, each element is assigned to another variable If X is a primitive type, element values are copied into item variable Fall 2010Sharif University of Technology18

Variable argument lists Sharif University of Technology19

Variable argument lists Sometimes they are called vararg Varargs are actually arrays Sharif University of Technology20

Where storage lives Registers Stack Heap Constants Non-RAM Sharif University of Technology21

Memory Hierarchy Sharif University of Technology22

Registers Fastest Inside the CPU Number of registers are limited You don’t have direct control over registers In assembly you have direct access to registers C and C++ have access to this storage to some extent Sharif University of Technology23

The Stack In RAM Slower than register but less limited Mechanism of function call in CPU Stack pointer (cp) Support of CPU Java references are (usually) placed on stack Primitive data types are also (usually) located in stack Java compiler must know the lifetime and size of all the items on the stack Java objects themselves are not placed on the stack Sharif University of Technology24

The stack (cont.) C++ allows allocation of objects on the stack E.g. this code creates an object on the stack Person p; In C++ it creates an object on the stack In Java it creates only a reference on the stack The actual object will be on Heap C++ allows arrays of known size on stack Java does not! Sharif University of Technology25

Compile time vs. Run time Some information are available at compile time Stack elements should be specified in compile time So C++ allows these variables on stack: int array[10]; Person p; Some information are not available at compile time So variable length variables can not be on stack If n is a variable “int array[n] “ is not allowed in C++ Java is simple! No object on stack! Sharif University of Technology26

The Heap This is a general-purpose pool of memory Also in the RAM area All Java objects live here The compiler doesn’t need to know the length of the variables new operator  the storage is allocated on the heap The objects may become garbage Garbage collection Sharif University of Technology27

Heap Generations The heap is split up into generations The young generation stores short-lived objects that are created and immediately garbage collected The Old generation Objects that persist longer are moved to the old generation also called the tenured generation The permanent generation (or permgen) is used for class definitions and associated metadata Sharif University of Technology28

Other storages Constant values are often placed directly in the program code Non-RAM Storage Streamed objects Persistent objects Sharif University of Technology29

Primitive Types new is not efficient for these small variables int a; char ch; In these cases, automatic variable is created that is not a reference The variable holds the value directly It’s placed on the stack Much more efficient These primitives are stored on stack, when…? When they are inside an object Sharif University of Technology30

Java Primitive Types Sharif University of Technology31

Primitive Wrapper Classes Used to represent primitive values when an Object is required All of them are immutable Java 5 added some shortcuts for their assignment Sharif University of Technology32

Example 1 Sharif University of Technology33 public static void main(String[] args) { A parent = new A(); } class A { B child = new B(); int e; } class B { int c; int d; }

Example 2 public static void foo(String bar){ Integer baz = new Integer(bar); } Sharif University of Technology34

Parameter Passing Styles Call by value Call by reference Call by pointer Java style : Call by passing value of references! Let’s see! Sharif University of Technology36

What happens in a method call Sharif University of Technology37

C++ Parameter Passing Call by value Call by pointer Call by reference Sharif University of Technology38

void cppMethod( Person byValue, Person*byPointer, Person& byReference){ byValue.name = "ali"; byPointer->name = "ali"; byReference.name = "ali"; } Person p1, p3; Person* p2; p2 = new Person(…); cppMethod(p1, p2, p3); Sharif University of Technology39 This is a C++ code This is NOT a java code! Does p1.name change? no Does p2->name change? yes Does p3.name change? yes

void cppMethod( Person byValue, Person*byPointer, Person& byReference){ Person* newP = new Person; byValue = *newP; byPointer = newP; byReference = *newP; } cppMethod(p1, p2, p3); Sharif University of Technology40 This is a C++ code This is NOT a java code! Does p1 change? no Does p2 change? no Does p3 change? yes

Java Parameter Passing Java has no pointer Java references are different from C++ references Java references are more like C++ pointers than C++ references A Java reference is something like a limited pointer Sharif University of Technology41

public void javaMethod( Person first, Person second, int number){ first.age = 12; number = 5; Person newP = new Person(); second = newP; } javaMethod(p1, p2, myInt); Sharif University of Technology42 Does p1.age change? yes Does myInt change? no Does p2 change? no In java, primitive variables are passed to methods by their values Reference values are passed by their reference values.

Swap Sharif University of Technology43

Swap (2) Sharif University of Technology44

Call by reference in C++ Sharif University of Technology45

Call by reference in C# Sharif University of Technology46

In java Everything is passed by value Primitive-types are passed by value References are passed by value But not the value of the object the value of the reference If you want to pass something by reference… Wrap it in an object And make it mutable Sharif University of Technology47

Sharif University of Technology48

Sharif University of Technology49