public class Person {
private String name;
private int age;
public Person(String name, int age) {
if (name == null) {
throw new IllegalArgumentException("Name cannot be null.");
}
if (age <= 0) {
throw new IllegalArgumentException("Age must be > 0");
}
this.name = name;
this.age = age;
}
@Override
public String toString() {
return this.name + ", " + this.age;
}
/**
* Check if this object is equal to the other object.
* @param other
*/
@Override
public boolean equals(Object other) {
if (other == null) {
return false;
}
if (!this.getClass().equals(other.getClass())) {
return false;
}
Person otherPerson = (Person) other;
return this.age == otherPerson.age &&
this.name.equals(otherPerson.name);
}
}
Finally, if all the previous checks pass, we know that we have two objects of the same type. Now we actually check if they have equal values for their instance variables.
Finally, if all the previous checks pass, we know that we have two objects of the same type. Now we actually check if they have equal values for their instance variables.
First, we check if the other object is null.
If it is null, then this object is clearly not equal to
the other object, and we return false.
First, we check if the other object is null.
If it is null, then this object is clearly not equal to
the other object, and we return false.
Next, we check if this object and the other object have the
same dynamic type. We get the type of each object
using the getClass method, and check if they the same.
If they aren't, the objects cannot be equal.
Next, we check if this object and the other object have the
same dynamic type. We get the type of each object
using the getClass method, and check if they the same.
If they aren't, the objects cannot be equal.
The equals method should be overridden for all classes that you create. It checks whether the calling object is equal to the argument.
The equals method should be overridden for all classes that you create. It checks whether the calling object is equal to the argument.
The equals method implements an equivalence relation
on non-null object references:
x, x.equals(x) should return
true.
x and y, x.equals(y)
should return true if and only if
y.equals(x) returns true.
x, y, and z, if
x.equals(y) returns true and
y.equals(z) returns true, then
x.equals(z) should return true.
x and y, multiple invocations of
x.equals(y) consistently return true
or consistently return false, provided no
information used in equals comparisons on the
objects is modified.
x,
x.equals(null) should return false.
An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.
hashCode
method whenever this method is overridden, so as to maintain the
general contract for the hashCode method, which states
that equal objects must have equal hash codes.equals method for class Object implements
the most discriminating possible equivalence relation on objects;
that is, for any non-null reference values x and
y, this method returns true if and only
if x and y refer to the same object
(x == y has the value true).
In other words, under the reference equality equivalence
relation, each equivalence class only has a single element.obj - the reference object with which to compare.true if this object is the same as the obj
argument; false otherwise.Object. The returned
Class object is the object that is locked by
static synchronized methods of the represented class.
Object. The returned
Class object is the object that is locked by
static synchronized methods of the represented class.
The actual result type is Class<? extends |X|>
where |X| is the erasure of the static type of the
expression on which getClass is called. For
example, no cast is required in this code fragment:
Number n = 0;
Class<? extends Number> c = n.getClass();
Class object that represents the runtime
class of this object.String class represents character strings. All
string literals in Java programs, such as "abc", are
implemented as instances of this class.
String class represents character strings. All
string literals in Java programs, such as "abc", are
implemented as instances of this class.
Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared. For example:
String str = "abc";
is equivalent to:
char data[] = {'a', 'b', 'c'};
String str = new String(data);
Here are some more examples of how strings can be used:
System.out.println("abc");
String cde = "cde";
System.out.println("abc" + cde);
String c = "abc".substring(2, 3);
String d = cde.substring(1, 2);
The class String includes methods for examining
individual characters of the sequence, for comparing strings, for
searching strings, for extracting substrings, and for creating a
copy of a string with all characters translated to uppercase or to
lowercase. Case mapping is based on the Unicode Standard version
specified by the Character class.
The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. For additional information on string concatenation and conversion, see The Java Language Specification.
Unless otherwise noted, passing a null argument to a constructor
or method in this class will cause a NullPointerException to be
thrown.
A String represents a string in the UTF-16 format
in which supplementary characters are represented by surrogate
pairs (see the section Unicode
Character Representations in the Character class for
more information).
Index values refer to char code units, so a supplementary
character uses two positions in a String.
The String class provides methods for dealing with
Unicode code points (i.e., characters), in addition to those for
dealing with Unicode code units (i.e., char values).
Unless otherwise noted, methods for comparing Strings do not take locale
into account. The Collator class provides methods for
finer-grain, locale-sensitive String comparison.
javac compiler
may implement the operator with StringBuffer, StringBuilder,
or java.lang.invoke.StringConcatFactory depending on the JDK version. The
implementation of string conversion is typically through the method toString,
defined by Object and inherited by all classes in Java.Since other classes have their own equals methods, we should use those
to compare data of other reference types.
true if and only if the argument is not null and is a
String object that represents the same sequence of characters as this
object.
Since other classes have their own equals methods, we should use those
to compare data of other reference types.
true if and only if the argument is not null and is a
String object that represents the same sequence of characters as this
object.
For finer-grained String comparison, refer to
Collator.
This same check could be done using the instanceof operator.
That is, you could rewrite this line as
other instanceof Person
However, instanceof checks if an object is a child type
of the right-hand-side, whereas using getClass lets us check
if this and the other object are an exact match.
Object. The returned
Class object is the object that is locked by
static synchronized methods of the represented class.
This same check could be done using the instanceof operator.
That is, you could rewrite this line as
other instanceof Person
However, instanceof checks if an object is a child type
of the right-hand-side, whereas using getClass lets us check
if this and the other object are an exact match.
Object. The returned
Class object is the object that is locked by
static synchronized methods of the represented class.
The actual result type is Class<? extends |X|>
where |X| is the erasure of the static type of the
expression on which getClass is called. For
example, no cast is required in this code fragment:
Number n = 0;
Class<? extends Number> c = n.getClass();
Class object that represents the runtime
class of this object.This method signature (return type, name, and parameter type) must
match the signature for the equals method from the Object class.
Otherwise, it will not override the Object::equals method.
Object is the root of the class hierarchy.
Every class has Object as a superclass. All objects,
including arrays, implement the methods of this class.This method signature (return type, name, and parameter type) must
match the signature for the equals method from the Object class.
Otherwise, it will not override the Object::equals method.
IllegalArgumentException with the
specified detail message.IllegalArgumentException with the
specified detail message.s - the detail message.IllegalArgumentException with the
specified detail message.IllegalArgumentException with the
specified detail message.s - the detail message.