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.