Download presentation
Presentation is loading. Please wait.
1
By Mr. Muhammad Pervez Akhtar pervezbcs@gmail.com http://pervez.farooka.com
2
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
3
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
4
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
5
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
6
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
7
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
8
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
9
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
10
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
11
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
12
The meaning of ‘=‘ suppose we want to create two buttons Then what is wrong???
13
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
14
Parameter Passing Only reference of the object will be passed
15
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
16
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.