Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Object in oops → Object in oops

Object in oops

Object in oops

In Java, Object-Oriented Programming (OOP) is a programming paradigm that revolves around the concept of objects. An object is an instance of a class, which is a blueprint for creating objects

Object Identity and Object Comparison

Object Identity refers to the unique identity of an object. It determines whether two objects share the same memory address. In Java, identity is tested with the == operator. For example, if a == b, then a and b are identical. Object Comparison refers to the process of comparing two objects to determine if they are equal. Two objects can be equal but not identical. In Java, equality is defined by the equals() method. If two objects are equal, then they contain the same state. However, two objects that are identical are always equal. For instance, consider the following code snippet:
Object Comparison example public class Main{ public static void main(String args[]){ String str1 = "Hello"; String str2 = "Hello"; String str3 = new String("Hello"); System.out.println(str1 == str2); // true System.out.println(str1 == str3); // false System.out.println(str1.equals(str3)); // true } }


true false true
In this example, str1 and str2 are identical because they share the same memory address. Therefore, str1 == str2 returns true. However, str1 and str3 are not identical because they have different memory addresses. Therefore, str1 == str3 returns false. However, str1 and str3 are equal because they contain the same state. Therefore, str1.equals(str3) returns true. .equals() is a method to check both the objects are equal or not. Here are some of the methods provided by the Object class: equals(Object obj): This method is used to compare two objects for equality. It returns true if the objects are equal; otherwise, it returns false. hashCode(): This method returns the hash code value for the object. The hash code is used by some algorithms to optimize the performance of collections. toString(): This method returns a string representation of the object. The string representation can be used for debugging or logging purposes. getClass(): This method returns the class object of the object. The class object can be used to get information about the class, such as its name, superclass, and interfaces. clone(): This method creates a new object that is a copy of the original object. The new object is a shallow copy, which means that the object’s fields are copied, but the objects referred to by the fields are not copied. finalize(): This method is called by the garbage collector before an object is destroyed. It can be overridden by subclasses to perform cleanup operations, such as closing files or releasing resources. The Object class also provides two protected methods: clone() and finalize(). These methods are intended to be overridden by subclasses, but they are not intended to be called directly by client code.