Objects and Memory Mehdi Einali Advanced Programming in Java 1.

Slides:



Advertisements
Similar presentations
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Advertisements

CSCI 1100/ , 6.2, 6.4 April 12, 15, 17.
Today Quiz solutions are posted on the Grading page. Assignment 1 due today, 7pm. Arrays as Pointers, Cont. Aliasing & Passing by Reference null Winter.
Designing an ADT The design of an ADT should evolve naturally during the problem-solving process Questions to ask when designing an ADT What data does.
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.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
Road Map Introduction to object oriented programming. Classes
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.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Memory and C++ Pointers.  C++ objects and memory  C++ primitive types and memory  Note: “primitive types” = int, long, float, double, char, … January.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Review of C++ Programming Part II Sheng-Fang Huang.
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.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Presented by: Mojtaba Khezrian. Agenda Object Creation Object Storage More on Arrays Parameter Passing For Each VarArgs Spring 2014Sharif University of.
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
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.
1 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
Peyman Dodangeh Sharif University of Technology Fall 2013.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
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.
Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Peyman Dodangeh Sharif University of Technology Spring 2014.
CSE 1201 Object Oriented Programming ArrayList 1.
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.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
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.
Today Review passing by reference and pointers. null pointers. What is an Object? Winter 2016CMPE212 - Prof. McLeod1.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
Computer Organization and Design Pointers, Arrays and Strings in C
Computer Science 210 Computer Organization
Intro To Classes Review
objects CIS 421 Kutztown University
Lecture 6 C++ Programming
Advanced Programming Behnam Hatami Fall 2017.
Computer Science 210 Computer Organization
Pointers and References
CSC 253 Lecture 8.
CSC 253 Lecture 8.
اشیاء در جاوا Java Objects
Fall 2018 CISC124 2/22/2019 CISC124 Quiz 1 This Week. Topics and format of quiz in last Tuesday’s notes. The prof. (me!) will start grading the quiz.
Java Programming Language
Winter 2019 CMPE212 5/10/2019 CMPE212 – Reminders
Pointers and References
Classes and Objects Object Creation
SPL – PS2 C++ Memory Handling.
CMSC 202 Constructors Version 9/10.
Presentation transcript:

Objects and Memory Mehdi Einali Advanced Programming in Java 1

2 agenda Java memory structure Object Creation Object Storage More on Arrays Parameter Passing For Each VarArgs

3 Quiz(7min) 1)What is key differences between object oriented programming language and its ancestors? 2)What does encapsulation means?

4 Java memory structure

5 Computer memory hierarchy

6 Java memory Stack is LIFO Heap is just addressable memory Perm gen is for definitions Moved to jvm mem in java 8

7 Memory management in stack

8 Memory management

9

10

11

12

13

14

15

16

17

18

19

20

21 Object creation

22 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 Class Declaration

23 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

24 new operator new creates a new object from specified type new String(); new Book(); new int(); Primitive types are not referenced

25 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();

26 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

27 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 Or static value in case of String: String str=“salam”;

28 new new creates a piece of memory Returns its reference Where is the piece of memory? In Heap …

29 Object storage

30 Memory management in heap

31

32

33

34

35

36

37 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

38 Primitive-Type Array Sample

39 More on arrayes

40 Arrays in memory

41

42

43

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

45 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; }

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

47 Question? 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(); }

48 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

49 Garbage collection

50

51

52

53 What is the output of this code?

54 aliasing

55

56

57

58 Parameter passing

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

60 What happens in a method call

61 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

62 public void javaMethod( Person first, Person second, int number){ first.age = 12; number = 5; Person newP = new Person(); second = newP; } javaMethod(p1, p2, myInt); 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.

63 Java reference vs c++ pointer Java references are strongly typed You can't do pointer arithmetic with java references Those differences make C pointers more powerful, but also more dangerous References might be implemented by storing the address

64 Swap

65 Swap (2)

66 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

67

68 foreach

69 For Each

70 Foreach on array In for each expression, each element is assigned to another variable

71 Foreach on array Element values are copied into item variable (value for primitives and reference value for objects)

72 varargs

73 Variable argument lists

74 Variable argument lists Sometimes they are called vararg Varargs are actually arrays

75 quiz

76 Wrapper classes

77 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

78 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?

79 end