Object Class
Contains methods that are implemented by all Apex types.
All Apex classes have the Object class as the base class, and therefore implement all the Object class methods.
The following are methods for Object.
-
Compares an object to the specified object and returns true if both are equal. Otherwise, returns false.
-
Returns a hash code for the object.
-
Returns a string that represents the object. The string includes the class name of which the object is an instance, the at (@) character, and the unsigned hexadecimal representation of the object’s hash code value.
Compares an object to the specified object and returns true if both are equal. Otherwise, returns false.
public Boolean equals(Object obj)
- obj: Type: Object The object with which to compare.
Type: Boolean
If x, y, and z are non-null instances of a class, the equals method must be:
- Reflexive:
x.equals(x) - Symmetric:
x.equals(y)returnstrueif and only ify.equals(x)returnstrue - Transitive: If
x.equals(y)returnstrueandy.equals(z)returnstrue, thenx.equals(z)returnstrue - Consistent: Multiple invocations of
x.equals(y)consistently returntrueor consistently returnfalse, provided the objects used in comparison are not modified. - For any non-null reference value x,
x.equals(null)returnsfalse
Use the equals method in your class to simplify comparision of objects. You can use the == operator to compare objects, or the equals method. For example:
Returns a hash code for the object.
public Integer hashCode()
Type: Integer
-
If the
hashCodemethod is invoked on the same object more than once during execution of an Apex request, it must return the same value.- The hash code value is same provided no information used in equals comparisons on the object is modified.
- The hash code value need not remain consistent from one Apex execution request to another execution of the same application.
-
If two objects are equal, based on the
equalsmethod,hashCodemust return the same value. -
If two objects are unequal, based on the result of the
equalsmethod, it is not required thathashCodereturn distinct values.
Returns a string that represents the object. The string includes the class name of which the object is an instance, the at (@) character, and the unsigned hexadecimal representation of the object’s hash code value.
public String toString()
Type: String
In API version 57.0 and later, the toString() method only includes member variables of Apex objects that are visible in the current namespace. Non-global properties are suppressed from output when you invoke toString() on managed Apex types. To keep the non-global state of the object visible in debug output, you can explicitly override the toString() method.