Home »
Java programming language
Java StackTraceElement equals() method with example
StackTraceElement Class equals() method: Here, we are going to learn about the equals() method of StackTraceElement Class with its syntax and example.
Submitted by Preeti Jain, on December 24, 2019
StackTraceElement Class equals() method
- equals() method is available in java.lang package.
- equals() method is used to check whether the given object is an instance of other StackTraceElement denoting the same starting point as this instance.
- equals() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
- equals() method does not throw an exception at the time of comparing two Objects.
Syntax:
public boolean equals(Object obj2);
Parameter(s):
- Object obj2 – represents the Object to compare with.
Return value:
The return type of this method is boolean – it returns a boolean value based on the given cases,
- It returns true when the given object is an instance of StackTraceElement denoting the same execution point.
- It returns false when the given object is not an instance of StackTraceElement denoting the same execution point.
Example:
// Java program to demonstrate the example
// of boolean equals(Object o) method of StackTraceElement
import java.io.*;
import java.util.*;
public class Equals {
public static void main(String args[]) {
// Create StackTraceElements
StackTraceElement ste1 = new StackTraceElement("st1", "method1", "StackTrace1.java", 2);
StackTraceElement ste2 = new StackTraceElement("st2", "method2", "StackTrace2.java", 1);
StackTraceElement ste3 = new StackTraceElement("st3", "method3", "StackTrace1.java", 2);
// By using getFileName() method is to retrieve
// the file names
Object ob1 = ste1.getFileName();
Object ob2 = ste2.getFileName();
Object ob3 = ste3.getFileName();
// It returns true when both objects are the same
boolean b1 = ob1.equals(ob2);
boolean b2 = ob1.equals(ob3);
// Display status
System.out.println("Is ob1 and ob2 are same :" + " " + b1);
System.out.println("Is ob1 and ob3 are same :" + " " + b2);
}
}
Output
Is ob1 and ob2 are same : false
Is ob1 and ob3 are same : true