Podcast
Questions and Answers
Study Notes
Objects
- An object has a unique identity, state, and behaviors.
- The state of an object consists of a set of data fields (also known as properties) with their current values.
- The behavior of an object is defined by a set of methods.
Classes and Objects
- A class is a template that defines the properties and behavior of an object.
- Multiple objects can be created from a single class.
- Each object has its own state (data fields with values) but shares the same behavior (methods) as other objects of the same class.
Example: Person Class
- A
Person
class has a data field calledname
and a method calledgetName
. - Multiple objects can be created from the
Person
class, each with its ownname
value (e.g.,O1
withname
"Ahmad",O2
withname
"Zaki", etc.).
Characteristics of Objects
- An object represents a real-world entity that can be distinctly identified (e.g., a student, a desk, a circle, a button, a loan).
- An object has both a state (data fields with values) and behavior (methods).
Garbage Collection
- When an object is no longer referenced, it is considered garbage.
- The JVM automatically collects garbage.
Assigning null to a Reference Variable
- If an object is no longer needed, you can explicitly assign null to a reference variable for the object.
- This helps in garbage collection.
Variables of Primitive Data Types and Object Types
- Primitive type variables hold values (e.g., int i = 1).
- Object type variables hold references to objects (e.g., Circle c = new Circle()).
- Reference variables hold the memory address of an object.
Copying Variables of Primitive Data Types and Object Types
- Primitive type assignment (e.g., i = j) copies the value.
- Object type assignment (e.g., c1 = c2) copies the reference, not the object.
- After object type assignment, both variables point to the same object.
Garbage Collection Example
- After the assignment statement c1 = c2, the object previously referenced by c1 is no longer referenced and becomes garbage.
Garbage Collection
- When an object is no longer referenced, it is considered garbage.
- The JVM automatically collects garbage.
Assigning null to a Reference Variable
- If an object is no longer needed, you can explicitly assign null to a reference variable for the object.
- This helps in garbage collection.
Variables of Primitive Data Types and Object Types
- Primitive type variables hold values (e.g., int i = 1).
- Object type variables hold references to objects (e.g., Circle c = new Circle()).
- Reference variables hold the memory address of an object.
Copying Variables of Primitive Data Types and Object Types
- Primitive type assignment (e.g., i = j) copies the value.
- Object type assignment (e.g., c1 = c2) copies the reference, not the object.
- After object type assignment, both variables point to the same object.
Garbage Collection Example
- After the assignment statement c1 = c2, the object previously referenced by c1 is no longer referenced and becomes garbage.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
Learn about the fundamentals of object-oriented programming, including objects, classes, state, and behavior. Understand how classes define properties and behavior, and how multiple objects can be created from a single class.