Android Guides | Samples

Java.Lang.Object Class

The root class of the Java class hierarchy.

See Also: Object

Syntax

[Android.Runtime.Register("java/lang/Object", DoNotGenerateAcw=true)]
public class Object : IJavaObject, IJavaObjectEx, IDisposable

Remarks

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

Follow this style to write a canonical equals method:

java Example

   // 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.

Follow this style to write a canonical hashCode method:

java Example

   @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:

java Example

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

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

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

java Example

   @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.

[Android Documentation]

Requirements

Namespace: Java.Lang
Assembly: Mono.Android (in Mono.Android.dll)
Assembly Versions: 0.0.0.0
Since: Added in API level 1

The members of Java.Lang.Object are listed below.

See Also: Object

Public Constructors

A constructor used when creating managed representations of JNI objects; called by the runtime.

Protected Constructors

Constructs a new instance of Object.

Public Properties

[read-only]
ClassClass. Returns the unique instance of Class that represents this object's class.
[read-only]
HandleIntPtr. The handle to the underlying Android instance.

Protected Properties

[read-only]
ThresholdClassIntPtr. This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.
[read-only]
ThresholdTypeType. This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

Public Methods

Dispose()
Equals(Object) : Boolean
Compares this instance with the specified object and indicates if they are equal.
Finalize()
static
FromArray<T>(T[]) : Object
override
GetHashCode() : Int32
Returns an integer hash code for this object.
static
GetObject<T>(IntPtr, JniHandleOwnership) : T
Wraps a JNI object reference into a fully typed IJavaObject implementation.
static
GetObject<T>(IntPtr, IntPtr, JniHandleOwnership) : T
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.
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.
ToArray<T>() : T[]
override
ToString() : String
Returns a string containing a concise, human-readable description of this object.
Wait()
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object.
Wait(Int64)
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the specified timeout expires.
Wait(Int64, Int32)
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the specified timeout expires.

Protected Methods

Clone() : Object
Creates and returns a copy of this Object.
Dispose(Boolean)
JavaFinalize()
Invoked when the garbage collector has detected that this instance is no longer reachable.
SetHandle(IntPtr, JniHandleOwnership)
Sets the Object.Handle property.

Public Operators

static
Conversion to Java.Lang.Object[](Explicit)
static
Conversion to System.Boolean(Explicit)
static
Conversion to System.Boolean[](Explicit)
static
Conversion to System.Byte[](Explicit)
static
Conversion to System.Char(Explicit)
static
Conversion to System.Char[](Explicit)
static
Conversion to System.Double(Explicit)
static
Conversion to System.Double[](Explicit)
static
Conversion to System.Int32(Explicit)
static
Conversion to System.Int32[](Explicit)
static
Conversion to System.Int64(Explicit)
static
Conversion to System.Int64[](Explicit)
static
Conversion to System.SByte(Explicit)
static
Conversion to System.Single(Explicit)
static
Conversion to System.Single[](Explicit)
static
Conversion to System.String(Explicit)
static
Conversion to System.String[](Explicit)
static
Conversion to Java.Lang.Object(Implicit)
static
Conversion to Java.Lang.Object(Implicit)
static
Conversion to Java.Lang.Object(Implicit)
static
Conversion to Java.Lang.Object(Implicit)
static
Conversion to Java.Lang.Object(Implicit)
static
Conversion to Java.Lang.Object(Implicit)
static
Conversion to Java.Lang.Object(Implicit)
static
Conversion to Java.Lang.Object(Implicit)
static
Conversion to Java.Lang.Object(Implicit)
static
Conversion to Java.Lang.Object(Implicit)
static
Conversion to Java.Lang.Object(Implicit)
static
Conversion to Java.Lang.Object(Implicit)
static
Conversion to Java.Lang.Object(Implicit)
static
Conversion to Java.Lang.Object(Implicit)
static
Conversion to Java.Lang.Object(Implicit)
static
Conversion to Java.Lang.Object(Implicit)
static
Conversion to Java.Lang.Object(Implicit)

Explicitly Implemented Interface Members