Java - Character.Subset equals() Method



The Java Character.Subset equals() method compares two Subset objects for equality. The result of this method will be true only if the object reference of this object and the argument object is the same.

Since we are using the final access specifier for this method, the same return value will be guaranteed for all the subclasses of the Subset class.

Note − This method uses the inheritance concept to create a Subset object as the constructor in the Character.Subset class has protected access. Therefore, Character.Subset class is extended from another public class whose constructor uses super() method to invoke the Object class that creates an object.

Syntax

Following is the syntax for Java Character.Subset equals() method

public final boolean equals(Object obj)

Parameters

  • obj − This is the reference object with which to compare.

Return Value

This method returns true if this object is the same as the obj argument, else false.

Example

In the following example, we instantiate CharacterSubsetDemo class objects. The method Java Character.Subset equals() is then called on these objects to check whether they are equal or not.

package com.tutorialspoint;
import java.lang.*;
public class CharacterSubsetDemo extends Character.Subset {
  
   // constructor of super class
   CharacterSubsetDemo(String s) {
      super(s); // invokes the immediate parent class: Object
   }
   public static void main(String[] args) {
      CharacterSubsetDemo obj1 = new CharacterSubsetDemo("admin");
      CharacterSubsetDemo obj2 = new CharacterSubsetDemo("webmaster");
      CharacterSubsetDemo obj3 = new CharacterSubsetDemo("administrator");

      // compares Subset objects for equality
      boolean retval = obj1.equals(obj1);
      System.out.println("Object obj1 is equal to obj1 ? " + retval);
      retval = obj2.equals(obj1);
      System.out.println("Object obj1 is equal to obj2 ? " + retval);
      retval = obj3.equals(obj1);
      System.out.println("Object obj1 is equal to obj3 ? " + retval);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Object obj1 is equal to obj1 ? true
Object obj1 is equal to obj2 ? false
Object obj1 is equal to obj3 ? false

Example

Another example to check whether two given Subset objects, one object referencing the other Subset object, are equal or not, using the equals() method is given below.

import java.lang.*;
public class CharacterSubsetDemo extends Character.Subset {
  
   // constructor of super class
   CharacterSubsetDemo(String s) {
      super(s); // immediate parent class invoked to create object
   }
   public static void main(String[] args) {
      CharacterSubsetDemo obj1 = new CharacterSubsetDemo("admin");
      CharacterSubsetDemo obj2 = obj1;

      // compares Subset objects for equality
      boolean val = obj2.equals(obj1);
      System.out.println("Object 1 is equal to Object 2 ? " + val);
   }
}

Output

Let us compile and run the program above and the output is displayed as −

Object 1 is equal to Object 2 ? true

Example

Following is another example to show the working of the Java Character.Subset equals() method. Two different Subset objects are instantiated with the same String value; the method is invoked on these objects to check whether they are equal or not. As the object references are different, even though they hold the same value, the method returns false.

import java.lang.*;
public class CharacterSubsetDemo extends Character.Subset {   
   CharacterSubsetDemo(String s) {
      super(s);
   }
   public static void main(String[] args) {
      CharacterSubsetDemo obj1 = new CharacterSubsetDemo("23AS&%");
      CharacterSubsetDemo obj2 = new CharacterSubsetDemo("23AS&%");

      // compares Subset objects for equality
      boolean value = obj2.equals(obj1);
      System.out.println("Object 1 is equal to Object 2 ? " + value);
   }
}

Output

Once we compile and run the program above, the output is displayed as follows. Even though the string name is the same, the output is achieved as false −

Object 1 is equal to Object 2 ? false

Example

Following is an example to show the usage of this method when the object argument is passed to it as null −

import java.lang.*;
public class CharacterSubsetDemo extends Character.Subset {    
   CharacterSubsetDemo(String s) {
      super(s);
   }
   public static void main(String[] args) {
      CharacterSubsetDemo obj1 = new CharacterSubsetDemo(null);
      CharacterSubsetDemo obj2 = new CharacterSubsetDemo("23AS&%");

      // compares Subset objects for equality
      boolean res_value = obj2.equals(obj1);
      System.out.println("Object 1 is equal to Object 2 ? " + res_value);
   }
}

Exception

Once the program is compiled and run, the output will print a NullPointerException as follows −

Exception in thread "main" java.lang.NullPointerException: nameat java.lang.Character$Subset.<init>(Character.java:615)
   at CharacterSubsetDemo.<init>(CharacterSubsetDemo.java:6)
   at CharacterSubsetDemo.main(CharacterSubsetDemo.java:11)
java_lang_character.subset.htm
Advertisements