Object
and its methodsObject
is the root class of all other Java classes (including
arrays).
Object
is located in the java.lang
package.
Object
declares the following methods, which all other classes
inherit:
protected Object clone()
boolean equals(Object obj)
protected void finalize()
Class<?> getClass()
int hashCode()
String toString()
void notify()
void notifyAll()
void wait()
void wait(long timeout)
void wait(long timeout, int nanos)
These methods allow you to perform special tasks in your Java classes.
Object
You can extend Object
explicitly:
public class Employee extends Object {
String name;
public Employee(String name) {
this.name = name;
}
public String getName() {
return name;
}
public static void main(String[] args) {
Employee e = new Employee("John");
System.out.println(e.getName());
}
}
Or implicitly:
public class Employee {
private String name;
// ...
}
Less is more in this case, so pick the implicit alternative.
getClass()
The getClass()
method returns the runtime class of the object on
which this method is called.
Someclass s = new Someclass();
Class c = s.getClass();
The runtime class is represented by a Class
object (from the java.lang
package).
A Class
object is sort of a meta object describing the class of an
object.
It contains the following data about the class:
See the documentation here.
Create the class Something
:
int a
+ getter/setterCreate a class Runner
with a main method that:
Something
getClass()
getDeclaredFields()
You can find the solution to this exercise here.
How do we achieve a true distinct copy of an object?
clone()
The clone()
method creates and returns a copy of the object on
which it is called.
It always returns an Object
, hence it must be
cast to the object's actual type
To create a clone, write:
aCloneableObject.clone();
The Object
's implementation of this method checks to see
whether the object on which clone()
was invoked implements the
Cloneable
interface. If it does not, the method throws a
CloneNotSupportedException
.
The default implementation of clone()
:
The simplest way to make your class cloneable is to
Cloneable
interface and clone()
by invoking super.clone()
.@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
You can also write you own implementation of clone()
.
Something
from the last exercise
public Object clone()
Runner
class that
Something
Something
You can find the solution to this exercise here.
We can compare reference variables using:
When you use ==
on primitive variables, it
performs a content equality check.
int x = 10;
int y = 10;
System.out.println("x==y: " + (x == y));
// => x==y: true
When you use ==
on reference variables, it
performs a referential equality check.
Person a = new Person("John", 30);
Person b = new Person("John", 30);
System.out.println("a==b: " + (a == b));
// => a==b: false
What if we want to compare objects by their content?
equals()
The equals()
method lets you compare the contents of two objects
to see if they are equal.
Object
's default implementation of equals()
performs a referential equality check.
To perform content equality, we need to override equals()
.
The rules for overriding this method are stated in Oracle's official documentation for the Object
class.
x.equals(x)
should return true.x.equals(y)
should return true if and only if y.equals(x)
returns true.x.equals(y)
returns true and y.equals(z)
returns true, then x.equals(z)
should return true.x.equals(y)
consistently return true or consistently return
false, provided no information used in equals comparisons on the objects is modified.x.equals(null)
should
return false.When overriding equals()
, it's common practice to also override
the hashCode()
method.
Here is how you can compare the contents of two objects:
class Employee {
private String name;
private int age;
public Employee(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof Employee)) return false;
Employee e = (Employee) obj;
return e.getName().equals(name) && e.getAge() == age;
}
}
The Java instanceof
operator is used to test whether the object is
an instance of the specified type (class or subclass or interface).
public class Equals {
public static void main(String[] args) {
Employee e1 = new Employee("John", 30);
Employee e2 = new Employee("John", 30);
System.out.println("Is e1 equals to e1: " + e1.equals(e1));
System.out.println("Is e1 equals to e2: " + e1.equals(e2));
System.out.println("Is e2 equals to e1: " + e2.equals(e1));
Employee e3 = new Employee("John", 35);
System.out.println("Is e1 equals to e3: " + e1.equals(e3));
System.out.println("Is e3 equals to e1: " + e3.equals(e1));
Employee e4 = new Employee("Jane", 30);
System.out.println("Is e1 equals to e4: " + e1.equals(e4));
System.out.println("Is e4 equals to e1: " + e4.equals(e1));
}
}
Is e1 equals to e1: true
Is e1 equals to e2: true
Is e2 equals to e1: true
Is e1 equals to e3: false
Is e3 equals to e1: false
Is e1 equals to e4: false
Is e4 equals to e1: false
Write a class Land
, containing:
Override the equals()
method in such a way that returns
true if the area (width*length) is equal in both objects, e.g. Land(10,10) = Land(20,5)
You can find the solution to this exercise here.
toString()
The toString()
method returns a string representation of the
object on which the method is called.
It is quite useful for debugging purposes.
By default, toString()
return a value in the format classname@hashcode
, where hashcode is shown in hexadecimal notation.
Employee me = new Employee("John", 30);
System.out.println(me);
Employee@424058530
To get more meaningful messages, we can override this method.
public class Employee {
private String name;
private int age;
public Employee(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
Employee{name='John', age=30}