Java StackTraceElement equals() Method



Description

The Java StackTraceElement equals() method returns true if the specified object is another StackTraceElement instance representing the same execution point as this instance.

Declaration

Following is the declaration for java.lang.StackTraceElement.equals() method

public boolean equals(Object obj)

Parameters

obj − This is the object to be compared with this stack trace element.

Return Value

This method returns true if the specified object is another StackTraceElement instance representing the same execution point as this instance.

Exception

NA

Example: Checking StackTraceElement with another object for Equality

The following example shows the usage of Java StackTraceElement getClassName() method. In this program, we've defined a function named function2() which compares the StackTraceElement of current thread with an object with value "a". Another function named function1() is used to instantiate the StackTraceElementDemo class and call the function2() method. In main method, we've called the function1() method and result is printed.

package com.tutorialspoint;

public class StackTraceElementDemo {

   public static void main(String[] args) {
      function1();
   }
 
   public static void function1() {
      new StackTraceElementDemo().function2();
   }
 
   public void function2() {
      int i;  
    
      // ob is the object to be compared with this stack trace element
      Object ob = "a";
      System.out.println(Thread.currentThread().getStackTrace()[0].
      equals(ob));
   }
}  

Output

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

false

Example: Checking StackTraceElement of ArithmeticException with self for Equality

The following example shows the usage of Java StackTraceElement getClassName() method. In main method, we've created three int variables where one variable is carrying zero value. In try-catch block, we've created a divide by zero scenario to raise an ArithmeticException. In catch block, we've handled the ArithmeticException, retrieved an array of StackTraceElement from it using getStackTrace() method and then using equals() method on first element of the array, it is compared with same object and result is printed.

package com.tutorialspoint;

public class StackTraceElementDemo {

   public static void main(String[] args) {

      // variables to store int values
      int a = 0, b = 1, c;

      try {
         // being divide by zero, this line will throw an ArithmeticException
         c = b / a;
      }catch(ArithmeticException e) {
         // get the array of StackTraceElement
         StackTraceElement[] stackTraceElements =  e.getStackTrace();
         StackTraceElement element = stackTraceElements[0];
         // compare the objects
         System.out.println(stackTraceElements[0].equals(element));
      }
   } 
}  

Output

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

true

Example: Checking StackTraceElement of Exception with self for Equality

The following example shows the usage of Java StackTraceElement getClassName() method. In main method, we've created three int variables where one variable is carrying zero value. In try-catch block, we've created a divide by zero scenario to raise an ArithmeticException. In catch block, we've handled the Exception, retrieved an array of StackTraceElement from it using getStackTrace() method and then using equals() method on first element of the array, it is compared with a different object and result is printed.

package com.tutorialspoint;

public class StackTraceElementDemo {

   public static void main(String[] args) {

      // variables to store int values
      int a = 0, b = 1, c;

      try {
         // being divide by zero, this line will throw an ArithmeticException
         c = b / a;
      }catch(Exception e) {
         // get the array of StackTraceElement
         StackTraceElement[] stackTraceElements =  e.getStackTrace();
         StackTraceElement element = stackTraceElements[0];
         // compare the objects
         System.out.println(stackTraceElements[0].equals(element));
      }
   } 
}  

Output

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

true
java_lang_stacktraceelement.htm
Advertisements