You are on page 1of 8

OBJECT ORIENTED CONCEPTS

Part II

DISCLAIMER

This document does not claim any originality and cannot be used
as a substitute for prescribed textbooks. The information
presented here is just for the completion of teaching
assignments. Contents of various sources as mentioned at the end
of the document were taken verbatim for preparing this document.
The ownership of the information lies with the respective
authors or institutions.

Sudhakar Singh
Department of Electronics and Communication
University of Allahabad
Object Oriented Concepts
Part – II

Reference Semantics

Note: Here all the concepts of this topic will be demonstrated by examples of Java Language.

1. Value semantics
Value semantics is employed for only built-in simple types, such as integers and real numbers
that are not class types. All primitive types in Java use value semantics. It is a behavior where
values are copied when assigned, passed as parameters, or returned. When one variable is
assigned to another, its value is copied. Modifying the value of one variable does not affect
others.

For examples:

int x = 5;
int y = x; // x = 5, y = 5
y = 17; // x = 5, y = 17
x = 8; // x = 8, y = 17

2. Reference semantics
Java, C#, Visual Basic, and Python employ reference semantics for “objects”. It is a behavior
where variables actually store the address of an object in memory. When one variable is assigned
to another, the object is not copied; both variables refer to the same object. Modifying the value
of one variable will affect others.

Arrays and objects use reference semantics. The reason is efficiency and sharing.
Copying large objects slows down a program so, references are efficient. Further, it's useful to
share an object's data among methods.
For examples:

int[] a1 = {4, 15, 8};


int[] a2 = a1; // refer to same array as a1
a2[0] = 7;
System.out.println(Arrays.toString(a1)); // [7, 15, 8]

2.1 References and objects

To understand the reference semantics, we must understand the difference between an object,
and object reference.

Example 1: Consider this Java code (in some method).

Point a;
Create a new object reference a (on the run-time stack)
[1 object reference, 0 objects]

a = new Point(3,4);
Create a new object (on the heap). This object is anonymous (Let’s call it Object #1). Assign a to
refer to Object #1.
[1 object reference, 1 object]

Point b = a;
Create a new object reference b (on the run-time stack). Assign b to refer to Object #1.
[2 object references, 1 object]
Point c = new Point(7,5);
Create a new object reference c (on the run-time stack). Create a new object (on the heap). This
is another anonymous object (Let’s call it Object #2). Assign c to refer to Object #2.
[3 object references, 2 objects]

a = c;
Assign a to refer to Object #2.
[3 object references, 2 objects]

b = new Point(7,5);
Create a new object (on the heap). This is another anonymous object (Let’s call it Object #3).
Assign b to refer to Object #3.
[3 object references, 3 objects; but object #1 is inaccessible, and subject to deletion by the
system]

System.out.println( (a == b) ? 'e' : 'n' );


Prints 'n'. The equality operator compares object references, not objects. a refers to object #2; b
to object #3.

System.out.println( (a == c) ? 'e' : 'n' );


Prints 'e'. a and c refer to the same object (object #3).

Example 2:
Example 3:

2.2 Some differences between objects and object references, in Java

Characteristics Object References Objects


(comparison parameters)
Where allocated On the run-time stack On the heap.
(unless object reference is an
element of an array, or a field
within an object).
Amount of memory Independent of the type of the Depends on type of the object.
object (typically 4 bytes).
Name Has a name Always anonymous
(unless it is an element of an array,
or a field within an object).
Refers to / Referred to by Refers to a single object, unless it Zero or more object references
is null. may refer to it. (If 0, the object
is eligible for deletion.)
Type(s) Has two types: Has only one type
(i) a declared type (static type), and (actual type = declared type).
(ii) an actual type (dynamic type).
Modified by Assigning to the object reference. Invoking a mutating method
of the object.
Modification disallowed Declaring the object reference as Defining no mutating methods
by final. in the object’s class.
When deleted When control leaves the block in By the system, when the
which it is defined. (However, if garbage collection process
the object reference is an element detects that no references to
of an array, or field within an the object remain.
object, it is deleted when the
containing array or object is
deleted.)

The actual type of an object reference is the type of the object to which it refers. The
actual type must be a subclass of the declared type. For example, in
Token t = new BinaryOp();
the declared type of t is Token, and the actual type is BinaryOp.

2.3 Some More Examples

Example: Objects as parameters

When an object is passed as a parameter, the object is not copied. The parameter refers to the
same object. If the parameter is modified, it will affect the original object.

Example: Arrays pass by reference

Arrays are passed as parameters by reference. Changes made in the method are also seen by the
caller.
2.4 Null references

The null value does not refer to any object. For example,

Point a; // a is referring to null

Another example, the elements of an array of objects are initialized to null.

String[] words = new String[5];

It is not the same as the empty string "" or the string "null".

For another example, the unset reference fields of an object are initialized to null.

public class Student {


String name;
int id;
}
Student student = new Student();

2.5 Dereferencing
It is used to access data or methods of an object, and done with the dot notation, such as
s.length(). When you use a dot (.) after an object variable, Java goes to the memory for that
object and looks up the field/method requested.

Student student = new Student();


student.name = "Stuart";
String s = student.name.toUpperCase();

2.6 Null pointer exception

It is illegal to dereference null (it causes an exception). The null does not refer to any object; it
has no methods or data.

Student student = new Student();


String s = student.name.toUpperCase(); // ERROR

Output:
Exception in thread "main"
java.lang.NullPointerException
at Example.main(Example.java:8)

REFERENCES

1 https://labmap.ime.usp.br/~yoshi/2015i/mac323/sandbox/2015.03.10/IntroCS/reference_s
emantics.pdf
2 https://courses.cs.washington.edu/courses/cse143/17sp/lectures/04-
07/slides/references.pdf

You might also like