Pages

Tuesday 25 December 2012

Java Notes : Objects

  1. Thejava.langpackage is indispensable when programming in Java. It is automat-ically imported into every source file at compile time. The package contains the
    Objectclass that is the superclass of all classes,
  2.  A class declaration, without the extendsclause, implicitly extends the Objectclass
  3. int hashCode()
    When storing objects in hash tables, this method can be used to get a hash
    value for an object. This value is guaranteed to be consistent during the execu-tion of the program. This method returns the memory address of the object as
    the default hash value of the object
  4. boolean equals(Object obj)
    Object reference and value equality are discussed together with the ==and !=
    operators (see Section 5.11, p. 191). The equals()method in the Objectclass
    returns trueonly if the two references compared denote the same object. The
    equals()method is usually overridden to provide the semantics of object value
    equality, as is the case for the wrapper classes and the Stringclass. For a
    detailed discussion of the equals()method
  5. final Class<?> getClass()
    Returns the runtime classof the object, which is represented by an object of the
    classjava.lang.Classat runtime
  6. protected Object clone() throws CloneNotSupportedException
    New objects that are exactly the same (i.e., have identical states) as the current
    object can be created by using the clone()method, i.e., primitive values and
    reference values are copied. This is called shallow copying. A class can override
    this method to provide its own notion of cloning. For example, cloning a com-posite object by recursively cloning the constituent objects is called deep copying.
    When overridden, the method in the subclass is usually declared publicto
    allow any client to clone objects of the class. If the overriding clone()method
    in the subclass relies on the clone()method in the Objectclass (i.e., a shallow
    copy), the subclass must implement the Cloneablemarker interface to indicate
    that its objects can be safely cloned. Otherwise, the clone()method in the
    Objectclass will throw a checked CloneNotSupportedException.
  7. String toString()
    If a subclass does not override this method, it returns a textual representation
    of the object, which has the following format:
    "<name of the class>@<hash code value of object>"
    Since the default hash value of an object is its memory address, this value is
    printed as a hexadecimal number, e.g., 3e25a5. This method is usually overrid-den and used for debugging purposes. The method call  Sys-tem.out.println(objRef)will implicitly convert its argument to a textual
    representation by calling the toString()method on the argument

No comments:

Post a Comment