Pointers Prasun Dewan Comp 114.

Slides:



Advertisements
Similar presentations
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Advertisements

ACM/JETT Workshop - August 4-5, Classes, Objects, Equality and Cloning.
Primitives, References and Wrappers Java has the following types defined as part of the java language; int float double byte char boolean long short These.
Introduction to Programming Lecture 39. Copy Constructor.
Chapter 3 DATA: TYPES, CLASSES, AND OBJECTS. Chapter 3 Data Abstraction Abstract data types allow you to work with data without concern for how the data.
Lecture # 21 Chapter 6 Uptill 6.4. Type System A type system is a collection of rules for assigning type expressions to the various parts of the program.
Written by: Dr. JJ Shepherd
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.
Java Syntax Primitive data types Operators Control statements.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Objects vs. Primitives Primitive Primitive byte, short, int, long byte, short, int, long float, double float, double char char boolean boolean Object (instance.
Inheritance and interfaces A class C1 is derived from class C2, then C1 is called subclass, and C2 is called superclass Superclass-parent, base class Subclass.
Java Data Types  Everything is an Object  Except Primitive Data Types  For efficiency  Platform independent  Portable  “slow”  Objects are often.
Primitive Values Vs Objects Wrapper Classes Memory Representation of Primitive Values Vs Objects –Pointers Equal Vs == Sharing of Objects Garbage Collection.
March 2005Java Programming1. March 2005Java Programming2 Why Java? Platform independence Object Oriented design Run-time checks (fewer bugs) Exception.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Objects Real and Java COMP.
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
Chapter 11 Abstract Classes and Interfaces 1. Abstract method New modifier for class and method: abstract An abstract method has no body Compare: abstract.
BPJ444: Business Programming using Java Java basics Tim McKenna
1 Java Console I/O Introduction. 2 Java I/O You may have noticed that all the I/O that we have done has been output The reasons –Java I/O is based on.
© A+ Computer Science - public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part C Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Generic Programming  Object Type  Autoboxing  Bag of Objects  JCL Collections  Nodes of Objects  Iterators.
Some Standard Classes Goals The Object class The String class Wrapper classes The Math class Random Numbers.
Copyright Curt Hill Variables What are they? Why do we need them?
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
Abstract Classes and Interfaces Chapter 9 CSCI 1302.
Lecture 9.4 Java Interfaces. © 2006 Pearson Addison-Wesley. All rights reserved Java does not support multiple inheritance. Interface Characteristics...
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
U n i v e r s i t y o f H a i l 1 ICS 202  2011 spring  Data Structures and Algorithms 
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
Print Me Who’s Data? First Executed Name Me My Mistake Q $100 Q $200 Q $300 Q $400 Q $500 Q $100 Q $200 Q $300 Q $400 Q $500 Final Jeopardy.
Duke CPS From C++ to Java l Java history: Oak, toaster-ovens, internet language, panacea l What it is ä O-O language, not a hybrid (cf. C++)
TeachJava! 2003 Corky Cartwright Dung Nguyen Stephen Wong Charlie Reis, James Hsia, Peter Centgraf.
ITK 168 – More Variables 10/13/05. Another example of using instance variables and constants  Go through SimpleBot  Modify SimpleBot to “teleport”
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Written by: Dr. JJ Shepherd
Chapter 5 Defining Classes II Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
 Variables can store data of different types, and different data types can do different things.  PHP supports the following data types:  String  Integer.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
C OMP 401 C OPY : S HALLOW AND D EEP Instructor: Prasun Dewan.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
Sections Basic Data Structures. 1.5 Data Structures The way you view and structure the data that your programs manipulate greatly influences your.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
JAVA MULTIPLE CHOICE QUESTION.
Object-oriented Programming in Java
OOP Powerpoint slides from A+ Computer Science
Agenda Warmup Finish 2.4 Assignments
Intro To Classes Review
Basic notes on pointers in C
CMSC 202 Static Methods.
Unit-2 Objects and Classes
The Language Package.
Comp 401 Metamorphosis: Casting vs. Conversion
Arrays of Objects Fall 2012 CS2302: Programming Principles.
Classes and Objects 5th Lecture
Java Classes and Objects 3rd Lecture
Value Types and Reference Types
Arrays of Objects Fall 2012 CS2302: Programming Principles.
© A+ Computer Science - OOP © A+ Computer Science -
Recitation 7 October 7, 2011.
Java Programming Language
Variables and Computer Memory
Subtype Substitution Principle
Variables and Constants
Presentation transcript:

Pointers Prasun Dewan Comp 114

Pointers Primitive values vs. objects Wrapper Classes Memory Representation of Primitive Values Vs Objects Pointers Equal Vs == Sharing of Objects Garbage Collection Shallow vs. Deep copy

(new Integer(5)).toString() Wrapper Classes “Joe Doe”.toString() Object Vector vector = new Vector() Number Integer vector.addElement(“Joe Doe”) (new Integer(5)).toString() new Integer(5) 5 String AStringHistory vector.addElement(new Integer(5)) Double vector.elementAt(3).intValue() vector.elementAt(3) AStringDatabase wrapper class wrapper class 5.toString() char boolean double int vector.addElement(5)

Other Wrapper Classes Double Boolean Character Float, Short, Long public Double(double value) public double doubleValue() Boolean public Boolean(boolean value) public boolean booleanValue() Character public Character(char value) public char charValue() Float, Short, Long

Storing Primitive Values/Variables 8 5.5 int i = 5; same, double size memory address 5 16 same size 48 double d 5.5 double d = 5.5; 52 int i 5 double e = d; 80 double e 5.5

Storing Objects Values/Variables 5.5 Double@8 8 Integer I = new Integer(5) different sizes Double D = new Double(5.5) 5 16 Integer@16 48 Double D 8 variables same size memory address 60 Integer I 16

Structured Objects public class APoint implements Point { int x, y; public APoint (int initX, int initY) { x = initX; y = initY; } public int getX() {return x}; public void setX(int newVal) {x = newVal;} public int getY() {return y}; public void setY(int newVal) {y = newVal;}

Structured Objects 5.5 Double@8 8 5 16 Integer@16 48 Double D 60 public class APoint implements Point { // instance vars int x, y; //methods … } 5.5 Double@8 8 5 16 Integer@16 48 Double D 60 Integer I 16 Point p = new APoint(50, 100) 50 100 80 APoint@80 96 Point p 80

Superclass Constructor Inheritance public class ABoundedPoint extends APoint { APoint upperLeftCorner, lowerRightCorner; public ABoundedPoint (int initX, int initY, Point initUpperLeftCorner, Point initLowerRightCorner) { super(initX, initY); upperLeftCorner = initUpperLeftCorner; lowerRightCorner = initLowerRightCorner; } … Superclass Constructor

Inheritance 50 APoint@8 8 100 16 APoint@16 48 ABoundedPoint@48 75 8 16 public class APoint implements Point { int x, y; … } 50 APoint@8 8 100 16 APoint@16 public class ABoundedPoint extends APoint { Point upperLeftCorner ; Point lowerRightCorner; … } 48 ABoundedPoint@48 75 8 16 new ABoundedPoint(75, 75, new APoint(50,50), new APoint(100,100) )

Assignment of Object Variables Point p1 = new APoint(50, 50); 8 50 APoint@8 Point p2 = p1; 50 p1.setX(100); 16 Point p1 8 P1 P2 APoint@8 48 Point p2 8

Assignment of Object Variables Point p1 = new APoint(50, 50); 8 100 APoint@8 Point p2 = p1; 50 p1.setX(100); p2.getX() 100 16 8 Point p1 p1 = new APoint(200,200); P1 P2 APoint@8 48 Point p2 8

Assignment of Object Variables Point p1 = new APoint(50, 50); 8 100 APoint@8 Point p2 = p1; 50 p1.setX(100); p2.getX() 100 16 64 Point p1 p1 = new APoint(200,200); p2.getX() 100 p2 = p1; 48 8 Point p2 P1 P2 APoint@64 APoint@8 64 200 APoint@64 200

Assignment of Object Variables Point p1 = new APoint(50, 50); 8 100 APoint@8 Point p2 = p1; 50 p1.setX(100); p2.getX() 100 16 64 Point p1 p1 = new APoint(200,200); p2.getX() 100 p2 = p1; p2.getX() 200 48 64 Point p2 P1 P2 APoint@64 APoint@8 64 200 APoint@64 200 Garbage collected

== for Objects Point p1 = new APoint(200, 200); 8 200 APoint@8 p2 == p2  false Same physical object? 16 64 Point p1 48 8 Point p2 P1 P2 APoint@64 APoint@8 64 200 APoint@64 200

== for Objects Point p1 = new APoint(200, 200); 8 200 APoint@8 Point p2 = p1; 200 p2 == p2  true 16 8 Point p1 P1 P2 APoint@8 48 8 Point p2

== vs. Equal for Strings String s1 = “Joe Doe”; 8 Joe Doe String@8  false 16 64 String s1 s1.equals(s2)  true 48 8 String s2 S1 S2 String@64 String@8 64 Joe Doe String@64

== vs. equals() for Objects Point p1 = new APoint(200, 200); 8 200 APoint@8 Point p2 = new APoint(200, 200) 200 p2 == p2  false 16 64 Point p1 p1.equals(p2)  true 48 8 Point p2 64 200 APoint@64 public boolean equals(Point otherPoint) { return x == otherPoint.getX() && y == otherPoint.getY(); } 200

Copying Objects p1 = new APoint(200, 200); p2 = p1; p1.setX (100); What if we want copy rather than reference. The properties can be changed independently Backup

Copier does the work p1 = new APoint(200, 200); p2 = new APoint (p1.getX(), p1.getY()); p1.setX (100); What if we want copy rather than reference. The properties can be changed independently Backup

Copied object does the work p1 = new APoint(200, 200); p2 = p1.copy(); p1.setX (100); Class APoint defines: public Point copy() { return new APoint(x, y); } All copiers reuse the copy code

Bounded Point Copy? public ABoundedPoint copy() { return new ABoundedPoint (x, y, upperLeftCorner, lowerRightCorner); };

Shallow Copy

Deep Copy

Bounded Point Deep Copy public ABoundedPoint copy() { return new ABoundedPoint (x, y, upperLeftCorner.copy(), lowerRightCorner.copy()); };

Shallow vs. Deep Copy Shallow copy: Deep copy Copies the instance but not its components Deep copy Copies the instance and deep copies the components