Docsity

objected oriented programming notes, Slides of Object Oriented Programming

Partial preview of the text

Download objected oriented programming notes and more Slides Object Oriented Programming in PDF only on Docsity!

Advanced OOP Concepts

Object Oriented Programming

Objectives

  • (^) To understand the concept of reference variable and reference type
  • (^) To understand the difference between reference variable and object
  • (^) To understand the difference of heap and stack
  • (^) To use some utility classes provided by Java
  • (^) To understand the different type of class methods and variables.

Reference Variable

  • (^) A reference variable is a variable that stores the memory address (reference) of an object.
  • (^) It does NOT store actual data — it points to where the object is stored in memory (Heap).

Example of Reference Variable class Student { String name; } public class Main { public static void main(String[] args) { Student s1 = new Student(); // reference variable s1.name = "Ali"; System.out.println(s1.name); } }

  • Student → Reference type
  • s1 → Reference variable
  • new Student() → Creates object in Heap memory
  • s1 → Stores address of that object

Null Reference

  • (^) Student s1 = null; //s1 is not pointing to an object

Real-Life Analogy Think of:

  • Object = House 🏠
  • Reference Variable = Address 🏠 You don’t carry the house, you carry the address!

Reference Variables and Reference Types

  • (^) Simply, reference variable is an object and reference type is a class.
  • (^) Objects are accessed via object’s reference variables, which contain references to the objects.
  • (^) A class is a reference type, which means that a variable of the class type can reference an instance of the class.
  • (^) The following statement declares the variable myCircle to be of the Circle type: Circle myCircle;
  • (^) The next statement creates an object and assigns its reference to myCircle: myCircle = new Circle();
  • (^) a single statement can combines the declaration of reference variable, the creation of an object, and the assigning of an object reference as; Circle myCircle = new Circle();

Heap & Stack Memory

  • (^) Java Heap space is used by java runtime to allocate memory to Objects and JRE classes.
  • (^) Objects are created in heap memory but method frames are stored in Stack memory.
  • (^) Garbage Collection runs on the heap memory.
  • (^) Stack contains method specific values and references that are short-lived. Stack implements LIFO.
  • (^) Heap memory is shared by all threads of Java application but Stack memory is local to each thread.
  • (^) Size of heap space is much bigger than the small size of Stack in Java

Reference Variables

Object or Reference variable

  • (^) An object reference variable that appears to hold an object actually contains a reference to that object.
  • (^) Strictly speaking, an object reference variable and an object are different, but most of the time the distinction can be ignored.
  • (^) Therefore, for simplicity, myCircle can be called a Circle object rather than use the longer-winded description that myCircle is a variable that contains a reference to a Circle object.
  • (^) When you assign one object reference variable to another object reference variable, you are not creating a copy of the object, you are only making a copy of the reference.

Variables of Primitive Types and Reference

Types

  • (^) Every variable represents a memory location that holds a value.
  • (^) Declaring a variable is to tell the compiler what type of value the variable can hold.
  • (^) For a variable of a primitive type, the value is of the primitive type.
  • (^) For a variable of a reference type, the value is a reference to where an object is located.

Variables of Primitive Types and Reference Types

  • (^) When you assign one variable to another, the other variable is set to the same value.
  • (^) For a variable of a primitive type, the real value of one variable is assigned to the other variable.
  • (^) For a variable of a reference type, the reference of one variable is assigned to the other variable.

null

  • (^) If a data field of a reference type does not reference any object, the data field holds a special Java value, null.
  • null is a literal just like true and false. While true and false are Boolean literals, null is a literal for a reference type.
  • (^) The default value of a data field is null for a reference type.
  • (^) Java assigns no default value to a local variable inside a method. public class Test { public static void main(String[] args) { int x; // x has no default value System.out.println("x is " + x); // error } }

NullPointerException

  • NullPointerException is a common runtime error.
  • (^) It occurs when you invoke a method on a reference variable with a null value.
  • (^) Make sure you assign an object reference to the variable before invoking the method through the reference variable.