Sunday, February 15, 2009

object class

Java.lang.package contain several utility classes which are mandatory for writing simple to complex programs.There is no need to import java.lang.package as by default it is available to our program
Java.lang.object class:
It is super class of any java class .all the predefined classes are user defined classes either directly or indirectly extends java.lang.objectclass
This class contain several utility methods which can be applied on any java object.
.for every class parent class in object class
Class A extends B
// code
Means, Ie.for B,parent is object but for A parent is B only .so java supports multilevel inheritance
The following are the 11 methods present in object class
1).public string to string()
For string representation of object.
2)public native int hashcode()
Returns the hashcode of an object
3)public Boolean equals(object obj)
For comparing the current object with the specified object
4)protected native object clone() throws clonesoft support Exception
To produced cloned object
5)protected void finalize()throws throwable
6)public final native class getclass()
7)public final viod wait()throws interrupted exception
8)public final void wait(longms)throws interrupted exception
9)public final void wait(long ms,int ns)throws interrupted exception
10)public final native void notify()
11)public final native void notifyAll()

to String Example:
Class Sample
String name;int rollno;
Student(string name,int rollno)
Public static void main(string[]args)
Student s1=new student(“visvam”,101);
Public string tostring()
//return get class().getName()+’@’+
//return”this is student object”;
HashCode Example:
String s1=new string(”visvam”);
String s2=new string(“visvam”);
hashcode()---when ever an object is created jvm allocates a number for that object which is considered as hashcode().
Most of the cases the hashcode saving is unique for every object
Jvm uses this hashcode while saving objects to hashtable,hashmap or hashset.
Hashcode never represents the address of an object.
Oppropriate way of overriding of hashCode method:
Ex: class Student
String name,int rollno;
Student(string name,int rollno)
Public int hashcode()
return rollno
Public string tostring()
return name+”-----“+rollno;
Public static void main(string[]args)
String s1=new string(“java”,101);
System.out.println(s1); // java ...... 101
Public int hashcode()
retuen rollno;
It is highly recomonded to override hashcode .In our classes.overriding hashcode is appropriate if and only if every object we have to assign a different hashcode.This is helpful for the jvm to save object in hashcode,hashmap,hashset and serching also.
Student s1=new student(“viswam”,101);
Student s1=new student(“surya”,102);
Student s1=new student(“surya”,102);
System.out.println(s1==s2); // false
System.out.println(s1.equal(s2)); // false
System.out.println(s2==s3); // false
System.out.println(s2.equals(s3)); // false
System.out.println(s2==s2); // true
System.out.println(s2.equals(s2)); // true
= = Operator:
This is meant for reference comparision always s1==s2 is true if and only ig both s1 and s2 pointing to the same object on the heap.
Ex: string s1=”visvam”;
Thread t1=new Thread()
we can’t apply==operator for incomparable types.validation leads to CTE saying incomparable types.
S1==null is always “false”(no CTE&RTE)
equals()method :
The equals method available in object class meant for reference or address comparision only(similar to==operator)but,we can override equals() in our class for content comparision.
Overriding of .equals()
===> public Boolean equals(object o)
Int rollno=this.rollno;
Int rollno2=s2.rollno;
Return true;
return false;
===>then System.out.println(s2.equals(s3)); //true
System.out.println(s1.equals(s2)); // false
====> in string class. .Equals() method is already overridden for context comparision. Object class.equals method. Satisfy the following two conditions.
1.this is always for address comparision never rise CTE&RTEs even the arguments are different types.
If the arguments are different types .equals() is simply return false.
public boolean equals(object o)
Name 0=this.name1;
Student s=(student)0;
return true;
return false;
Catch(classcastException e)
return false;
Catch(nullpointerException e)
Return false;
Then==> s.o.pln(“null”));

Comparision between==Operator &.equal :
= = Operator :
1.we can apply for both primitation and object references
2. r1= =r2 is true if and only both r1,r2 pointingly to same object on the heap. ie ==is always for reference comparision.
3.we cant’t override for context comparision.
4.we can’t apply = = operator for different tpes of objects .incomparable types.
5.s1= =null is always false
1.we can apply only for object references default ,.equals() present in the object class is meant for address comparision onl
3.we can override .
4..equals()never release any CTE&RTEs even the arguments are different types .in that situation it will just simply return false.
5.s1.equals(null) gives false when handle NPE through catch
Relationship between = =and .Equals
1. if r1==r2 is true then r1.equals(r2) is always true.
2. if r2 = = is false then r1.equals(r2) may returns true.
Contract between hashcode and .equals()
If r1=equals(r2) is true,then r1.hashcode() = = r2.hashcode();
Ie .equalent objects should alwas have same hashcode.
If r1.equals(r2) is false ,then their hashcode may be same.
If R1.hashcode() = = r2.hashcode() may returns true
If R1.hashcode() = = r2.hashcode() may returns true ,then r1.equals(r2)may be true.
If R1.hashcode() = = r2.hashcode() may returns false,then r1.equals(r2)alwas false.
In order to satisfy the above contract we have to override hashcode() whenever we are overriding .equals()
Protected object clone() throws clone not supported exception.
This method can used to prouduced exactly duplicate copy of an object..
All the objects can’t produce cloned object ,only clonable objects can produce duplicate copies .
An object is said to be conable if and only if the corresponding class implements clonable interface.
By using the folling object class clone() method we can produced cloned objects
Protected object clone() throws clone supported exception
CheckedException so we should handle by using try catch or throws to the caller by using throws clause.


Post a Comment