Www.javacup.ir Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.

Slides:



Advertisements
Similar presentations
Programming and Data Structure
Advertisements

CSCI 1100/ , 6.2, 6.4 April 12, 15, 17.
SPL/2010 Pointers and Parameter Passing in C++ 1.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
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.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Memory and C++ Pointers.  C++ objects and memory  C++ primitive types and memory  Note: “primitive types” = int, long, float, double, char, … January.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Review of C++ Programming Part II Sheng-Fang Huang.
Pointers CSE 2451 Rong Shi.
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.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Presented by: Mojtaba Khezrian. Agenda Object Creation Object Storage More on Arrays Parameter Passing For Each VarArgs Spring 2014Sharif University of.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
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.
Copyright  Hannu Laine C++-programming Part 3 Hannu Laine.
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.
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.
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.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Object-Oriented Programming in C++
Dynamic memory allocation and Pointers Lecture 4.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Copyright Curt Hill Variables What are they? Why do we need them?
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Peyman Dodangeh Sharif University of Technology Spring 2014.
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.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Objects and Memory Mehdi Einali Advanced Programming in Java 1.
Computer Organization and Design Pointers, Arrays and Strings in C
objects CIS 421 Kutztown University
Lecture 6 C++ Programming
Advanced Programming Behnam Hatami Fall 2017.
CSC 253 Lecture 8.
CSC 253 Lecture 8.
اشیاء در جاوا Java Objects
Pointers, Dynamic Data, and Reference Types
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Java Programming Language
Defining Classes and Methods
Pointers, Dynamic Data, and Reference Types
SPL – PS2 C++ Memory Handling.
CMSC 202 Constructors Version 9/10.
Presentation transcript:

Sadegh Aliakbary

Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP is clearly noted as the source in the used case. JAVACUP shall not be liable for any errors in the content, or for any actions taken in reliance thereon. Please send your feedback to 2JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Agenda Object Creation Object Storage More on Arrays Parameter Passing For Each VarArgs 3JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

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 4 Class Declaration JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

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 5JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

new Operator new creates a new object from specified type new String(); new Book(); new int(); Primitive types are not referenced 6JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

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(); 7JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

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 8JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

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 9JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

new new creates a piece of memory Returns its reference Where is the piece of memory? In Heap Where is the Heap? Later… 10JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

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 11JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Primitive-Type Array Sample 12JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Array Samples 13JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Array References There is three type of variable in this code array reference array[i] references Initial value: null array[i] objects 14JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

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; } 15JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Array Sample Student[] students = new Student[10]; for (int i = 0; i < students.length; i++) { students[i] = new Student(); students[i].setId(i); } 16JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

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(); } 17JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Object Destruction Allocated memory should be released delete operator in C++ Problems with delete in C++ Error-Prone Segmentation Fault! Sometimes causes memory leak a program consumes memory but is unable to release it Complicated in many situations You don’t need it in java Garbage Collection 18JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Quiz! Write a java class for representing … 19JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

What is the output of this code? 20JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Example Object Abstraction Abstract Data Type Object Declaration (Class Declaration) public class Person { private String name; private int age; public void run(){...} public void talk(){...} } 21JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Example Object Instantiation new Person JafarAgha = new Person(); JafarAgha.setAge(50); JafarAgha.setName("Jafar"); JafarAgha.talk(); Person AzamKhanoom = new Person(); 22JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Objects in Memory 50 … 23 J|a|f|a|r JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Parameter Passing Styles Call by value Call by reference Call by pointer Java style : Call by passing value of references! Let’s see! 24JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

What happens in a method call 25JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

C++ Parameter Passing Call by value Call by pointer Call by reference 26JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

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); 27 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 JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

void cppMethod( Person byValue, Person*byPointer, Person& byReference){ Person* newP = new Person; byValue = *newP; byPointer = newP; byReference = *newP; } cppMethod(p1, p2, p3); 28 This is a C++ code This is NOT a java code! Does p1 change? no Does p2 change? no Does p3 change? yes JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

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 29JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

public void javaMethod( Person first, Person second, int number){ first.age = 12; number = 5; Person newP = new Person(); second = newP; } javaMethod(p1, p2, myInt); 30 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. JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Swap 31JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Swap (2) 32JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Call by reference in C++ 33JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Call by reference in C# 34JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

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 35JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

36JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

For Each 38JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

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 39JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Variable argument lists 40JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Variable argument lists Sometimes they are called vararg Varargs are actually arrays 41JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Quiz! 42JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

43JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Where storage lives Registers Stack Heap Constants Non-RAM 44JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Memory Hierarchy 45JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

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 46JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

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 47JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

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! 48JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

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! 49JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

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 50JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

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 51JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

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 When these primitives are not stored on stack? When they are inside an object 52JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Java Primitive Types 53JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

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 54JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Sample Integer i = new Integer(2); Integer j = new Integer(2); System.out.println(i==j); //Prints false. Why? i = j;//Reference Assignment i = 2;//OK. A new shortcut in Java5+ Long l = 2;//Syntax Error. Why? Long l = 2L;//OK l = i;//Syntax Error. Why? 55JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

56JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source