# Object

public class **Object** The root class of the Java class hierarchy. All non-primitive types
(including arrays) inherit either directly or indirectly from this class.

#### Writing a correct `equals` method

Follow this style to write a canonical `equals` method:

```
   // Use @Override to avoid accidental overloading.
   @Override public boolean equals(Object o) {
     // Return true if the objects are identical.
     // (This is just an optimization, not required for correctness.)
     if (this == o) {
       return true;
     }

     // Return false if the other object has the wrong type.
     // This type may be an interface depending on the interface's specification.
     if (!(o instanceof MyType)) {
       return false;
     }

     // Cast to the appropriate type.
     // This will succeed because of the instanceof, and lets us access private fields.
     MyType lhs = (MyType) o;

     // Check each field. Primitive fields, reference fields, and nullable reference
     // fields are all treated differently.
     return primitiveField == lhs.primitiveField &&
             referenceField.equals(lhs.referenceField) &&
             (nullableField == null ? lhs.nullableField == null
                                    : nullableField.equals(lhs.nullableField));
   }
 
```

If you override `equals`, you should also override `hashCode`: equal
instances must have equal hash codes.

See *Effective Java* item 8 for much more detail and clarification.

#### Writing a correct `hashCode` method

Follow this style to write a canonical `hashCode` method:

```
   @Override public int hashCode() {
     // Start with a non-zero constant.
     int result = 17;

     // Include a hash for each field.
     result = 31 * result + (booleanField ? 1 : 0);

     result = 31 * result + byteField;
     result = 31 * result + charField;
     result = 31 * result + shortField;
     result = 31 * result + intField;

     result = 31 * result + (int) (longField ^ (longField >>> 32));

     result = 31 * result + Float.floatToIntBits(floatField);

     long doubleFieldBits = Double.doubleToLongBits(doubleField);
     result = 31 * result + (int) (doubleFieldBits ^ (doubleFieldBits >>> 32));

     result = 31 * result + Arrays.hashCode(arrayField);

     result = 31 * result + referenceField.hashCode();
     result = 31 * result +
         (nullableReferenceField == null ? 0
                                         : nullableReferenceField.hashCode());

     return result;
   }
 
```

If you don't intend your type to be used as a hash key, don't simply rely on the default
`hashCode` implementation, because that silently and non-obviously breaks any future
code that does use your type as a hash key. You should throw instead:

```
   @Override public int hashCode() {
     throw new UnsupportedOperationException();
   }
 
```

See *Effective Java* item 9 for much more detail and clarification.

#### Writing a useful `toString` method

For debugging convenience, it's common to override `toString` in this style:

```
   @Override public String toString() {
     return getClass().getName() + "[" +
         "primitiveField=" + primitiveField + ", " +
         "referenceField=" + referenceField + ", " +
         "arrayField=" + Arrays.toString(arrayField) + "]";
   }
 
```

The set of fields to include is generally the same as those that would be tested
in your `equals` implementation.

See *Effective Java* item 10 for much more detail and clarification.

### Public Constructor Summary

|---|---|
|   | [Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#Object())() Constructs a new instance of `Object`. |

### Public Method Summary

|---|---|
| boolean | [equals](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#equals(java.lang.Object))([Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) obj) Compares this instance with the specified object and indicates if they are equal. |
| final [Class](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Class.html)\<?\> | [getClass](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#getClass())() Returns the unique instance of `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Class.html` that represents this object's class. |
| int | [hashCode](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#hashCode())() Returns an integer hash code for this object. |
| final void | [notify](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#notify())() Causes a thread which is waiting on this object's monitor (by means of calling one of the `wait()` methods) to be woken up. |
| final void | [notifyAll](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#notifyAll())() Causes all threads which are waiting on this object's monitor (by means of calling one of the `wait()` methods) to be woken up. |
| [String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) | [toString](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#toString())() Returns a string containing a concise, human-readable description of this object. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait(long,%20int))(long timeout, int nanos) Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object or until the specified timeout expires. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait(long))(long timeout) Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object or until the specified timeout expires. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait())() Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object. |

### Protected Method Summary

|---|---|
| [Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) | [clone](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#clone())() Creates and returns a copy of this `Object`. |
| void | [finalize](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#finalize())() Invoked when the garbage collector has detected that this instance is no longer reachable. |

## Public Constructors

#### public
**Object**
()

Constructs a new instance of `Object`.

## Public Methods

#### public boolean
**equals**
([Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) obj)

Compares this instance with the specified object and indicates if they
are equal. In order to be equal, `o` must represent the same object
as this instance using a class-specific comparison. The general contract
is that this comparison should be reflexive, symmetric, and transitive.
Also, no object reference other than null is equal to null.

The default implementation returns `true` only if `this ==
o`. See [Writing a correct
`equals` method](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#writing_equals)
if you intend implementing your own `equals` method.

The general contract for the `equals` and `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#hashCode()` methods is that if `equals` returns `true` for
any two objects, then `hashCode()` must return the same value for
these objects. This means that subclasses of `Object` usually
override either both methods or neither of them.

##### Parameters

| obj | the object to compare this instance with. |
|---|---|

##### Returns

- `true` if the specified object is equal to this `Object`; `false` otherwise.

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#hashCode()`

#### public final [Class](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Class.html)\<?\>
**getClass**
()

Returns the unique instance of `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Class.html` that represents this
object's class. Note that `getClass()` is a special case in that it
actually returns `Class<? extends Foo>` where `Foo` is the
erasure of the type of the expression `getClass()` was called upon.


As an example, the following code actually compiles, although one might
think it shouldn't:


    List<Integer> l = new ArrayList<Integer>();
       Class<? extends List> c = l.getClass();

<br />

##### Returns

- this object's `Class` instance.

#### public int
**hashCode**
()

Returns an integer hash code for this object. By contract, any two
objects for which `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#equals(java.lang.Object)` returns `true` must return
the same hash code value. This means that subclasses of `Object`
usually override both methods or neither method.

Note that hash values must not change over time unless information used in equals
comparisons also changes.

See [Writing a correct
`hashCode` method](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#writing_hashCode)
if you intend implementing your own `hashCode` method.

##### Returns

- this object's hash code.

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#equals(java.lang.Object)`

#### public final void
**notify**
()

Causes a thread which is waiting on this object's monitor (by means of
calling one of the `wait()` methods) to be woken up. If more than
one thread is waiting, one of them is chosen at the discretion of the
VM. The chosen thread will not run immediately. The thread
that called `notify()` has to release the object's monitor first.
Also, the chosen thread still has to compete against other threads that
try to synchronize on the same object.

This method can only be invoked by a thread which owns this object's
monitor. A thread becomes owner of an object's monitor

- by executing a synchronized method of that object;
- by executing the body of a `synchronized` statement that synchronizes on the object;
- by executing a synchronized static method if the object is of type `Class`.

<br />

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#notifyAll()`
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait()`
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait(long)`
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait(long,%20int)`
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Thread.html`

#### public final void
**notifyAll**
()

Causes all threads which are waiting on this object's monitor (by means
of calling one of the `wait()` methods) to be woken up. The threads
will not run immediately. The thread that called `notify()` has to
release the object's monitor first. Also, the threads still have to
compete against other threads that try to synchronize on the same object.

This method can only be invoked by a thread which owns this object's
monitor. A thread becomes owner of an object's monitor

- by executing a synchronized method of that object;
- by executing the body of a `synchronized` statement that synchronizes on the object;
- by executing a synchronized static method if the object is of type `Class`.

<br />

##### Throws

| [IllegalMonitorStateException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/IllegalMonitorStateException.html) | if the thread calling this method is not the owner of this object's monitor. |
|---|---|

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#notify()`
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait()`
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait(long)`
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait(long,%20int)`
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Thread.html`

#### public [String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html)
**toString**
()

Returns a string containing a concise, human-readable description of this
object. Subclasses are encouraged to override this method and provide an
implementation that takes into account the object's type and data. The
default implementation is equivalent to the following expression:

```
   getClass().getName() + '@' + Integer.toHexString(hashCode())
```

See [Writing a useful
`toString` method](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#writing_toString)
if you intend implementing your own `toString` method.

##### Returns

- a printable representation of this object.

#### public final void
**wait**
(long timeout, int nanos)

Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object or until the
specified timeout expires. This method can only be invoked by a thread
that owns this object's monitor; see `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#notify()` on how a thread
can become the owner of a monitor.

A waiting thread can be sent `interrupt()` to cause it to
prematurely stop waiting, so `wait` should be called in a loop to
check that the condition that has been waited for has been met before
continuing.

While the thread waits, it gives up ownership of this object's
monitor. When it is notified (or interrupted), it re-acquires the monitor
before it starts running.

A timeout of zero means the calling thread should wait forever unless interrupted or
notified.

##### Parameters

| timeout | the maximum time to wait in milliseconds. |
| nanos | the fraction of a millisecond to wait, specified in nanoseconds. |
|---|---|

##### Throws

| [IllegalArgumentException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/IllegalArgumentException.html) | if `millis < 0`, `nanos < 0` or `nanos > 999999`. |
| [IllegalMonitorStateException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/IllegalMonitorStateException.html) | if the thread calling this method is not the owner of this object's monitor. |
| [InterruptedException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/InterruptedException.html) | if the current thread has been interrupted. The interrupted status of the current thread will be cleared before the exception is thrown. |
|---|---|

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#notify()`
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#notifyAll()`
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait()`
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait(long,%20int)`
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Thread.html`

#### public final void
**wait**
(long timeout)

Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object or until the
specified timeout expires. This method can only be invoked by a thread
which owns this object's monitor; see `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#notify()` on how a thread
can become the owner of a monitor.

A waiting thread can be sent `interrupt()` to cause it to
prematurely stop waiting, so `wait` should be called in a loop to
check that the condition that has been waited for has been met before
continuing.

While the thread waits, it gives up ownership of this object's
monitor. When it is notified (or interrupted), it re-acquires the monitor
before it starts running.

A timeout of zero means the calling thread should wait forever unless interrupted or
notified.

##### Parameters

| timeout | the maximum time to wait in milliseconds. |
|---|---|

##### Throws

| [IllegalArgumentException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/IllegalArgumentException.html) | if `millis < 0`. |
| [IllegalMonitorStateException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/IllegalMonitorStateException.html) | if the thread calling this method is not the owner of this object's monitor. |
| [InterruptedException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/InterruptedException.html) | if the current thread has been interrupted. The interrupted status of the current thread will be cleared before the exception is thrown. |
|---|---|

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#notify()`
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#notifyAll()`
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait()`
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait(long,%20int)`
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Thread.html`

#### public final void
**wait**
()

Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object. This method can
only be invoked by a thread which owns this object's monitor; see
`https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#notify()` on how a thread can become the owner of a monitor.

A waiting thread can be sent `interrupt()` to cause it to
prematurely stop waiting, so `wait` should be called in a loop to
check that the condition that has been waited for has been met before
continuing.

While the thread waits, it gives up ownership of this object's
monitor. When it is notified (or interrupted), it re-acquires the monitor
before it starts running.

##### Throws

| [IllegalMonitorStateException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/IllegalMonitorStateException.html) | if the thread calling this method is not the owner of this object's monitor. |
| [InterruptedException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/InterruptedException.html) | if the current thread has been interrupted. The interrupted status of the current thread will be cleared before the exception is thrown. |
|---|---|

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#notify()`
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#notifyAll()`
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait(long)`
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait(long,%20int)`
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Thread.html`

## Protected Methods

#### protected [Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html)
**clone**
()

Creates and returns a copy of this `Object`. The default
implementation returns a so-called "shallow" copy: It creates a new
instance of the same class and then copies the field values (including
object references) from this instance to the new instance. A "deep" copy,
in contrast, would also recursively clone nested objects. A subclass that
needs to implement this kind of cloning should call `super.clone()`
to create the new instance and then create deep copies of the nested,
mutable objects.

##### Returns

- a copy of this object.

##### Throws

| [CloneNotSupportedException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/CloneNotSupportedException.html) | if this object's class does not implement the `Cloneable` interface. |
|---|---|

#### protected void
**finalize**
()

Invoked when the garbage collector has detected that this instance is no longer reachable.
The default implementation does nothing, but this method can be overridden to free resources.

Note that objects that override `finalize` are significantly more expensive than
objects that don't. Finalizers may be run a long time after the object is no longer
reachable, depending on memory pressure, so it's a bad idea to rely on them for cleanup.
Note also that finalizers are run on a single VM-wide finalizer thread,
so doing blocking work in a finalizer is a bad idea. A finalizer is usually only necessary
for a class that has a native peer and needs to call a native method to destroy that peer.
Even then, it's better to provide an explicit `close` method (and implement
`https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Closeable.html`), and insist that callers manually dispose of instances. This
works well for something like files, but less well for something like a `BigInteger`
where typical calling code would have to deal with lots of temporaries. Unfortunately,
code that creates lots of temporaries is the worst kind of code from the point of view of
the single finalizer thread.

If you *must* use finalizers, consider at least providing your own
`https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/ref/ReferenceQueue.html` and having your own thread process that queue.

Unlike constructors, finalizers are not automatically chained. You are responsible for
calling `super.finalize()` yourself.

Uncaught exceptions thrown by finalizers are ignored and do not terminate the finalizer
thread.

See *Effective Java* Item 7, "Avoid finalizers" for more.

##### Throws

| [Throwable](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Throwable.html) |   |
|---|---|