Java StackTraceElement equals() 方法



描述

Java StackTraceElement equals() 方法如果指定的对象是另一个表示与该实例相同执行点的 StackTraceElement 实例,则返回 true。

声明

以下是 java.lang.StackTraceElement.equals() 方法的声明

public boolean equals(Object obj)

参数

obj − 这是要与此堆栈跟踪元素进行比较的对象。

返回值

如果指定的对象是另一个表示与该实例相同执行点的 StackTraceElement 实例,则此方法返回 true。

异常

示例:检查 StackTraceElement 与另一个对象的相等性

以下示例演示了 Java StackTraceElement getClassName() 方法的使用。在此程序中,我们定义了一个名为 function2() 的函数,该函数将当前线程的 StackTraceElement 与值为“a”的对象进行比较。另一个名为 function1() 的函数用于实例化 StackTraceElementDemo 类并调用 function2() 方法。在 main 方法中,我们调用了 function1() 方法并打印结果。

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));
   }
}  

输出

让我们编译并运行上述程序,这将产生以下结果:

false

示例:检查 ArithmeticException 的 StackTraceElement 与自身的相等性

以下示例演示了 Java StackTraceElement getClassName() 方法的使用。在 main 方法中,我们创建了三个 int 变量,其中一个变量的值为零。在 try-catch 块中,我们创建了一个除以零的情况以引发 ArithmeticException。在 catch 块中,我们处理了 ArithmeticException,使用 getStackTrace() 方法从中检索了一个 StackTraceElement 数组,然后使用数组第一个元素上的 equals() 方法,将其与相同对象进行比较并打印结果。

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));
      }
   } 
}  

输出

让我们编译并运行上述程序,这将产生以下结果:

true

示例:检查 Exception 的 StackTraceElement 与自身的相等性

以下示例演示了 Java StackTraceElement getClassName() 方法的使用。在 main 方法中,我们创建了三个 int 变量,其中一个变量的值为零。在 try-catch 块中,我们创建了一个除以零的情况以引发 ArithmeticException。在 catch 块中,我们处理了 Exception,使用 getStackTrace() 方法从中检索了一个 StackTraceElement 数组,然后使用数组第一个元素上的 equals() 方法,将其与一个不同的对象进行比较并打印结果。

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));
      }
   } 
}  

输出

让我们编译并运行上述程序,这将产生以下结果:

true
java_lang_stacktraceelement.htm
广告

© . All rights reserved.