StackTraceElement equals(Object) in Java

equals(Object): This method is available in the java.lang.StackTraceElement class of Java.

Syntax:

boolean java.lang.StackTraceElement.equals(Object obj)

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

Two stack trace elements a and b are equal if and only if:

equals(a.getClassLoaderName(),b.getClassLoaderName()) && equals(a.getModuleName(), b.getModuleName()) && equals(a.getModuleVersion(), b.getModuleVersion()) && equals(a.getClassName(), b.getClassName()) && equals(a.getMethodName(), b.getMethodName()) equals(a.getFileName(), b.getFileName()) && a.getLineNumber() == b.getLineNumber() where equals has the semantics of Objects.equals.

Parameters: One parameter is required for this method.

obj: the object to be compared with this stack trace element.

Returns: true if the specified object is another StackTraceElement instance representing the same execution point as this instance.

Exceptions: NA

Approach

Java

package com.StackTraceElement;

public class StackTraceElementequals {
    public static void main(String[] args) {

        String declaringClass = "java.lang.String";
        String methodName = "length()";
        String fileName = "String.class";
        int lineNumber = 675;
        StackTraceElement stackTraceElement =
new StackTraceElement(declaringClass, methodName,
fileName, lineNumber);

        StackTraceElement stackTraceElement1 =
new StackTraceElement(declaringClass, methodName,
fileName, lineNumber);

        System.out.println(stackTraceElement.equals(stackTraceElement1));
    }
}

Output:

true


Some other methods of StackTraceElement

StackTraceElement(String, String, String, int)This method creates a stack trace element representing the specified execution point. The module name and module version of the stack trace element will be null.

StackTraceElement(String, String, String, String, String, String, int)This method creates a stack trace element representing the specified execution point.

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

getClassLoaderName()This method returns the name of the class loader of the class containing the execution point represented by this stack trace element.

getClassName()This method returns the fully qualified name of the class containing the execution point represented by this stack trace element.

getFileName()This method returns the name of the source file containing the execution point represented by this stack trace element.

getLineNumber()This method returns the line number of the source line containing the execution point represented by this stack trace element.

getMethodName()This method returns the name of the method containing the execution point represented by this stack trace element.

getModuleName()This method returns the module name of the module containing the execution point represented by this stack trace element.

getModuleVersion()This method returns the module version of the module containing the execution point represented by this stack trace element.

hashCode()This method returns a hash code value for this stack trace element.

isNativeMethod()This method returns true if the method containing the execution point represented by this stack trace element is a native method.

toString()This method returns a string representation of this stack trace element.

No comments:

Post a Comment