Java Basics:Java Library Methods-1

Java Object Methods

Java Object is the superclass for all the classes in Java. All the methods of Object class can be used by all the subclasses and arrays. The Object class provides different methods to perform different operations on objects. 

Java Object getClass()

The Java Object getClass() method returns the class name of the object.

The syntax of the getClass() method is:

object.getClass()

getClass() Parameters

The getClass() method does not take any parameters.


getClass() Return Values

  • returns the class of the object that calls the method

Example 1: Java Object getClass()

import java.util.ArrayList;

class Main {
  public static void main(String[] args) {

    // getClass() with Object
    Object obj1 = new Object();
    System.out.println("Class of obj1: " + obj1.getClass());

    // getClass() with String
    String obj2 = new String();
    System.out.println("Class of obj2: " + obj2.getClass());

    // getClass() with ArrayList
    ArrayList<Integer> obj3 = new ArrayList<>();
    System.out.println("Class of obj3: " + obj3.getClass());
  }
}

Output

Class of obj1: class java.lang.Object
Class of obj2: class java.lang.String
Class of obj3: class java.util.ArrayList

In the above example, we have used the getClass() method to get the name of the class. Here, we are able to call the getClass() method using the String and ArrayList object.

It is because String and ArrayList inherit the Object class.


Example 2: Call getClass() from Custom Class

class Main {
  public static void main(String[] args) {

    // create an object of the Main class
    Main obj = new Main();

    // call getClass() method from Main
    System.out.println(obj.getClass()); 
  }
}

Output

class Main

Here, we have created a class named Main. Note that we have called the getClass() method using the method of Main.

It is possible because Object class is the superclass of all the classes in Java.

Note: The Object class is the superclass for all the classes in Java. Hence, every class can implement the getClass() method.

Java Object equals()

The Java Object equals() method checks whether two objects are equal.

The syntax of the equals() method is:

object.equals(Object obj)

equals() Parameters

The equals() method takes a single parameter.

  • obj - object which is to be compared with the current object

equals() Return Values

  • returns true if two objects are equal
  • returns false if two objects are not equal

Note: In Java, if two reference variables refer to the same object, then the two reference variables are equal to each other.


Example 1: Java Object equals()

class Main {
  public static void main(String[] args) {

    // equals() method with Object class
    // create two objects
    Object obj1 = new Object();
    Object obj2 = new Object();

    // check if obj1 and obj2 are equal
    System.out.println(obj1.equals(obj2));  // false

    // assign obj1 to obj3
    Object obj3 = obj1;
    System.out.println(obj1.equals(obj3));  // true
  }
}

In the above examples, we have created objects of the Object class. Here, the equals() method is used to check if objects are equal to each other.


Exampl 2: equals() With String

class Main {
  public static void main(String[] args) {

    // equals() with String objects
    // create objects of string
    String obj1 = new String();
    String obj2 = new String();

    // check if obj1 and obj2 are equal
    System.out.println(obj1.equals(obj2));    // true

    // assign values to objects
    obj1 = "Java Programming";
    obj2 = "Python Programming";

    // again check if obj1 and obj2 are equal
    System.out.println(obj1.equals(obj2));    // false

  }
}

In the above example, we have used the equals() method to check if two objects obj1 and obj2 are equal.

Here, initially, both the newly created objects are null. Hence, the method returns true. However, when we assigned values to the objects. The method returns false.

It is because the String class overrides the equal() method so that the method compares the element of the object. Since value of obj1 and obj2 are different, the method returns false.

Note: The Object class is the superclass for all the classes in Java. Hence, every class and arrays can implement the equals() method.


Comments

Popular posts from this blog

Java OOPS:OOPS Concepts Overview

Java OOPS:Constructors in Java – A complete study!!

Java Basics:Data Types