By Mr. Muhammad Pervez Akhtar

Slides:



Advertisements
Similar presentations
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Advertisements

Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
CSCI 1100/ , 6.2, 6.4 April 12, 15, 17.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Chapter 10 Introduction to Arrays
Java Software Solutions
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Objectives Learn about objects and reference variables Explore how to use predefined methods in a program.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
References and Objects Weiss ch. 2. Objects Are structured regions of memory –Reside in heap Each object is an instance of a type –e.g. String, File,
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Object References. Objects An array is a collection of values, all of the same type An object is a collection of values, which may be of different types.
Reference Types. 2 Objectives Introduce reference types –class –array Discuss details of use –declaration –allocation –assignment –null –parameter –aggregation.
Chapter 15: Operator Overloading
C Arrays and Pointers In Java, pointers are easy to deal with –In fact, there is little that can go wrong in Java since pointer access is done for you.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
Lecture 2 Arrays, Pointers, and Structures. Objective In this chapter, we will discuss several concepts: Arrays (first-class arrays, using vector) Strings.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Pointer Data Type and Pointer Variables. Objectives: Pointer Data Type and Pointer Variables Pointer Declaration Pointer Operators Initializing Pointer.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Recap Visual Perception and Data Visualization Types of Information Display Examples of Diagrams used for Data Display Planning Requirement for Data Visualization.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Pointer and Array Lists Chapter 3, Summary CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and.
CS 31 Discussion, Week 8 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:00-1:00pm.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
Methods: A Deeper Look. Template for Class Definition public class { } A.Import Statement B.Class Comments C.Class Name D.Data members E.Methods (inc.
 Constructor  Finalize() method  this keyword  Method Overloading  Constructor Overloading  Object As an Argument  Returning Objects.
Chapter 5 Introduction to Defining Classes
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java.
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Week 12 Methods for passing actual parameters to formal parameters.
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.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
Chapter 2 Reference Types. Class : Point2D class Point2D { private double x,y; public Point2D(double xx,double yy) { x = xx ; y = yy ;} public void setX(double.
© 2004 Pearson Addison-Wesley. All rights reserved January 23, 2006 Creating Objects & String Class ComS 207: Programming I (in Java) Iowa State University,
(C) 2010 Pearson Education, Inc. All rights reserved.  Best way to develop and maintain a large program is to construct it from small, simple pieces,
Reference Types CSCI-1302 Lakshmish Ramaswamy. Reference Variables Java supports 8 primitive types All other types are reference types Reference variable.
Sections Basic Data Structures. 1.5 Data Structures The way you view and structure the data that your programs manipulate greatly influences your.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
You learned how to declare pointer variables how to store the address of a variable into a pointer variable of the same type as the variable how to manipulate.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
Computer Organization and Design Pointers, Arrays and Strings in C
Java Primer 1: Types, Classes and Operators
Java Review: Reference Types
Chapter 4 (part 2).
Java Classes Aliases & Null & This
Java Programming Language
Review for Midterm 3.
Introduction to Pointers
Presentation transcript:

By Mr. Muhammad Pervez Akhtar

 What is a reference type and value?  How reference types differ from primitive types  Basics of Objects and References  Object declaration, Garbage Collection, Operators = and ==  Parameter Passing, Operator Overloading

 A reference variable(often abbreviated as simply reference) in Java is a variable that somehow stores the memory address where an object resides  All types ( other than primitive types) in Java are reference types, including strings, arrays, and file streams

Memory Object 2 Object 1 Memory address 3 Pointers Objects are stored in memory at a specific address Reference types stores references/addresses of objects Point1, point2 and point3 are reference types that stores object1, object2 addresses respectively Reference types also stores in memory at some address

 A reference variable will always store  The memory address where some object is residing  The null, if it is not currently referencing any object  Two broad categories of operations applied on reference variables  One allows us to examine or manipulate the reference value Change the stored value, comparing reference variables  Other applied on objects being referenced Examine or change the internal state of object

 Two reference variables can’t be multiplied  P1*P2=?? (if P1=1024 and P2=1024)  Increment and decrement can’t be applied  P1++ ?? (if P1=1000 )  Arithmetic operations can’t be performed on reference variables  Reference types are different than primitive types  Reference types store object address while primitive types stores values  It is looks illogical to use arithmetic operators on reference types

 The only operators that are allowed for reference types  Assignment (=), equality comparison (==, !=)  There are only three basic operations that deals with the object that is being referenced  Apply a type conversion  Access an internal field or call a method via the dot operator (.)  Use the instanceof operator to verify that the stored object is of a certain type

 In Java, an object is an instance of any of the non-primitive types (classes)  The dot operator (.) is used to select a method that is applied to an object  double theArea = theCircle.area( );  theCircle is objct of Circle  If theCircle is stores to null, the above give NullPointerException  (.) operator can also be used to access individual components of an object

 Declaration of objects  Button b; //declaration of reference variable  The name that will store object reference  Declaration doesn’t provide any object itself  2 nd line will give NullPointerException because b is not referencing any object

 Garbage collection  When objects are not longer needed, they are destroyed  So the memory it consumes will automatically be reclaimed by another object  JVM ensures that only unreachable or unreferenced memory can be reclaimed to reference

 The meaning of ‘=‘  v=k; value of k copied to v for primitive variables  For objects, the meaning of ‘=‘ is the same: Stored values are copied  obj1=obj2; copy address of the object being referenced by obj2 in obj1; and only when both are compatible types  Means that obj1 and obj2 are referencing the same object

 The meaning of ‘=‘  suppose we want to create two buttons Then what is wrong???

 Parameter Passing Are passed by value or by reference  Primitive Types Parameters Primitive types arguments are passed by value to formal parameters of function Original values are copied to formal parameters and remain unchanged if any operation applied in function  Reference Type Parameters Reference type arguments are passed by reference to formal parameters of function Original values are not copied, just address of object is passed to formal parameters Any operation will change the original values in the object being passed to a function

 Parameter Passing Only reference of the object will be passed

 The meaning of ‘==‘  For Primitive Types  ‘==‘ is true if the stored values are identical  a==b is true if a=5 and b=5  For Reference Types  For reference types,==is true only if the two references reference the same object Yes a b c Button1 Button2 True becoz c and b are referencing to same object

 No operator overloading for objects  Like functions, operators can also be overloaded  Operator overloading means Using built-in operators (+,-,*,/) to perform a specific task when applied to objects rather than primitive values  Example of operator overloading in C++  Complex a (2, 4);  Complex b (3, 5);  Complex c = a + b;  Java does not allow operator overloading and  Multiple Inheritance  Pointer