Pages

Saturday 7 January 2012

Java toString method


java's toString method is useful for giving string description about a  class so it's better to override tostring method for every class
for example
Class A{
public String toString(){

return "sample class written by iTfinGer";
}
public static void main(String[] s){
A a=new A();
System.out.println(a);
}


}

In the above program it will print the String returned by toString method not the hashcode of the object
So the output for the above program is

sample class written by iTfinGer

-------------------------
Class A{

public static void main(String[] s){
A a=new A();
System.out.println(a);
}

}


but the above code prints the  Hashcode.

No comments:

Post a Comment