A Classe Object

A classe java.lang.Object é a raiz a partir da qual todas as classes são definidas. Desse modo, as definições dessa classe estão disponíveis para objetos de todas as demais classes.

O método equals, que permite comparar objetos por seus conteúdos, é um dos métodos definido nessa classe:
public boolean equals(Object obj)
Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation:

  • It is reflexive: for any reference value x, x.equals(x) should return true.
  • It is symmetric: for any reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any reference values 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 object is modified.
  • For any non-null reference value x, x.equals(null) should return false.

The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any 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).

Parameters:
obj - the reference object with which to compare.
Returns:
true if this object is the same as the obj argument; false otherwise.

O método clone() permite criar duplicatas de um objeto:
protected Object clone()
                throws CloneNotSupportedException
	  
Creates and returns a copy of this object. The precise meaning of "copy" may depend on the class of the object. The general intent is that, for any object x, the expression:
 x.clone() != x
		  
will be true, and that the expression:
 x.clone().getClass() == x.getClass()
		  
will be true, but these are not absolute requirements. While it is typically the case that:
 x.clone().equals(x)
		  
will be true, this is not an absolute requirement. Copying an object will typically entail creating a new instance of its class, but it also may require copying of internal data structures as well. No constructors are called.
Returns:
a clone of this instance.
Throws:
CloneNotSupportedException - if the object's class does not support the Cloneable interface. Subclasses that override the clone method can also throw this exception to indicate that an instance cannot be cloned.
OutOfMemoryError - if there is not enough memory.

O método toString() permite converter uma representação interna do objeto em uma string que pode ser apresentada ao usuário:
public String toString()
	  
Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

 getClass().getName() + '@' + Integer.toHexString(hashCode())
		  
Returns:
a string representation of the object.

O método getClass retorna um objeto que representa a classe à qual o objeto pertence:
  public final Class getClass()
	  
Returns the runtime class of an object. ...
Returns:
the object of type Class that represents the runtime class of the object.

A partir do objeto retornado, da classe java.lang.Class, é possível obter o nome da classe usando o método da classe Class getName(), que retorna uma string com o nome da classe.


© Ivan Luiz Marques Ricarte
DCA/FEEC/UNICAMP

Last modified: Fri Nov 5 15:32:12 EDT 1999