Labels

algorithms (22) Design Patterns (20) java (19) linux (14) Snippet (13) service mix (6) soa (4)

Java Object Class

it is the base class of all the classes in java

 Methods in Java Object class

  • hashCode:
    • the more unique the hashCode is the more efficient is the hash map/table
    • used to find the bucket in which an object goes into in a hash table.
    • more than one invocation of hashCode of the same object should return same Integer.
    • if two objects are equal according to equals method then their hashCode must be same.
  • equals:
    • is used in hashMap when two objects have same hashCode.
    • if two objects are equal their hashCode must be same.
    • it should be
      • reflexive : x.equals(x)
      • symmetric:  x.equals(y) and y.equals(x)
      • transtive: x.equals(y) y.equals(z)   implies => x.equals(z) 
      • consitent: x.equals(y)  should allways return same value.
      • for null it should be false: x.equals(null)  false
    • the default behaviour of equals = “==” i.e is object comparision
  • toString :
    • the default implementation is
    • classname  @ hex format of the hashCode.
    • its good to override toString .
    • also should be concise and informative.
  • clone:
    • to clone the object .
    • x.clone() != x
    • x.clone().getClass() == x.getClass() Not mandatory *
    • x.clone().equals(x) Not mandatory *
    • alternative to clone is copy constructor

No comments:

Post a Comment

Search 24 Bytes