Monday 23 April 2012

difference between two most toString() methods in java !

hello every one java is based on strings , means every thing which you see is presented by strings , and every thing is presented with the help of  "toString()" method . now see why this thing arose in my mind !



at the time of coding , i implement this easily but at the time of  run-time i found something which does not suits in my eyes so i tried to found the reason behind that output , but before this see the output !


see there are 4 lines and each one in presenting their output according themselves , but why , what is the problem with "System.out.println(xxx.toString());" , but the point is that there are few things which are noticeable in seeing , that ::
1st line is output of reference.toString() ;
2nd line is output of only class variable ;
3rd and 4rth are different from 1st one , 3rd and 4rth are output of  String.toString() ;

so , definitely the problem or solution is situated in the toString() method of reference classes , means toString() of String and toString() of now_my_own_rules class , now look at the two different methods of both classes .

first look at the toString() method of String class .


see the lined area , and see the line which is put inside the two black boxes , means when ever you enter any string in any String object , it will return the string itself .


now also see the , the lines added inside the boxes , here you will find the String representation of object , but here i override the toString() method thats why it is printing something else , otherwise it will print the class_name@hexadecial values ..


thanks ,
tushar ..

No comments: