Top Companies hiring on Shine.com. Click Here to apply !
Click Here to Submit Your Resume for MNC Jobs !
JavaKen.com

Go Back   JavaKen.com > Java Introduction > Java Language Basics/Fundamentals
Forgot User Name / Password ?


Reply
 
Thread Tools Display Modes
  #1  
Old 13 August 2009, 06:50 PM
naveenm_006 naveenm_006 is offline
Member
 
Join Date: 16 Sep 2008
Posts: 87
Default What is the purpose of toString()?

Click Here To Register & Submit Your Resume !

Hi,
What is the purpose of toString()? and where it is used if possible tell me with one example.

Regards,
Naveen.

Click Here To Register & Submit Your Resume !
Reply With Quote

  #2  
Old 14 August 2009, 11:29 AM
adityasharma00 adityasharma00 is offline
Junior Member
 
Join Date: 10 Sep 2008
Location: NCR
Posts: 12
Thumbs up Re: What is the purpose of toString()?

- toString() is a method of Object class.
- Any class can use toString() mehod for describe self.

Example :
Code:
/** ToStringExample.java */

   class calculateTax{ 
       int calculateTax (float income){
          ...........
       } 
       String toString() {
          S.o.p("Using this class we can calculate the tax. [More Information]");
       }
   }

    public class ToStringExample {
       public static void main(String[] args) {
         S.o.p(new calculateTax());
          // If class use toString() method then S.o.p will display the written information in this method
          // If class does not use toString() method then S.o.p will display [classname@hashCodeNo].
       }
    }
Reply With Quote
  #3  
Old 14 August 2009, 08:20 PM
soumya866 soumya866 is offline
Junior Member
 
Join Date: 12 Aug 2009
Posts: 25
Thumbs up Re: What is the purpose of toString()?

To provide our own meaningful string representation we should override toString() method in our class.
Whenever we are passing object reference to System.out.println internally jvm calls toString() method on that object reference. The resulting string will be displayed to console.
If we are not overriding toString() then the object class toString() will be called.
Ex:
class Student
{
String name;
int roll no;
Student(String name, int rollno)
{
this.name=name;
this.rollno=rollno;
public String toString()
{
return name+"------"+rollno;
}
public static void main(String[] args)
{
Student s1=new Student("aaa",111);
Student s2=new Student("bbb",151);
}
}
when u compile and run abve program the output is
aaa-----111
bbb-----151
if we are not overriding toString() u willl get some
Student@2bbb86..{Hash code}
Student@1a33f11{Hash code}
Reply With Quote
  #4  
Old 15 August 2009, 02:09 AM
khadangap khadangap is offline
Member
 
Join Date: 21 Jun 2009
Location: Marathali,Bangalore
Posts: 66
Default Re: What is the purpose of toString()?

Hi Friends,

From my experience when we write System.out.println("some string "+ referencevariable);

If the referenceVariable is String / primitive / wrapper type then it will call the toString() method of Object class. Which will return a String inatialised by "referenceVariable" . But when the reference variable is referencing to an user defiend class type such as public class Myclass{------ } then to print the object at first we have to override the toString() method of object class which will return the exact defination of the object otherwise you cant under stand the return string which is the combination of "classname + @ + something " which is not the exact defn of object

Example :
Code:
public class Myclass {
int a;
int b;
public Myclass(int a, int b){
this.a=a;
this.b=b;
}
/*public String toString(){                       //line no 7 ,8, 9
return " a= " +a + "and  b =" +b; 
} */
public static void main(String arg[]){
Myclass myclass = new Myclass(10,20);
System.out.println("my object is : "+myclass);
//here this will call the over ridden toString method of Myclass and will print the written value of the called method.
}
}
Note :

1. The above program will give the out put : My object is : Myclass@10b6269 which is not the required object defination.

2. By un commenting line no 7,8,9 ( THAT MEANS BY OVERRIDING TOsTRING)u will get the following OUT PUT
myclass object is : a = 10 and b = 20
Buttom line:

When you want to the real meaning of an object of an user defined type you have to over ride toString() method of Object class which ll written a strint as u described . other wise it will call the ToString() method of Object class which will return a not understanding string which is useless for us.

Hello friend I m very poor in english . So try 2 understan by running the code.
Reply With Quote
  #5  
Old 25 August 2009, 02:37 PM
pawan_mangal pawan_mangal is offline
Member
 
Join Date: 20 Sep 2008
Posts: 30
Blog Entries: 1
Cool Re: What is the purpose of toString()?

Hi Friends

String, Wrapper class already overridden the toString() method of Object class so why it Return meaningful representation of String and Wrapper Object.
Reply With Quote
  #6  
Old 26 August 2009, 12:49 AM
khadangap khadangap is offline
Member
 
Join Date: 21 Jun 2009
Location: Marathali,Bangalore
Posts: 66
Default Re: What is the purpose of toString()?

hi friend,

Ur arguement is right but i have mentioned that in case of user defined classes only we cannot get the exact defination of an object. Since String & wrapper classes are pre defind in API and though they overrides toString() we have no headach. According to the toString() of String And Wrapper classes they prints the string.

I think it will help u.
Reply With Quote
  #7  
Old 26 August 2009, 10:54 PM
pawan_mangal pawan_mangal is offline
Member
 
Join Date: 20 Sep 2008
Posts: 30
Blog Entries: 1
Default Re: What is the purpose of toString()?

ya definatelly but so many classes in java api do not have implementation of toString() of Object class like -what about BufferedReader (any I/O Related class) and also HashMap (may be).

Am I right ?
Reply With Quote
  #8  
Old 26 August 2009, 11:53 PM
khadangap khadangap is offline
Member
 
Join Date: 21 Jun 2009
Location: Marathali,Bangalore
Posts: 66
Default Re: What is the purpose of toString()?

Hi friend ,

Thanks to give me suggetion but I m confused what u going to tell me ? Please clarify it .
Reply With Quote
  #9  
Old 27 August 2009, 07:32 PM
kirankumarm kirankumarm is offline
Junior Member
 
Join Date: 23 Aug 2008
Posts: 12
Default Re: What is the purpose of toString()?

Object : Object is a super class for all the classes.

The methods in Object class as follows :

Object clone( )
final void notify( )
Int hashCode( )
Boolean equals( )
final void notify( )
Void finalize( )
String toString( )
Final Class getClass( )
final void wait( )

toString() : An instance method that converts the object to a string.
Reply With Quote
  #10  
Old 30 August 2009, 10:18 AM
pawan_mangal pawan_mangal is offline
Member
 
Join Date: 20 Sep 2008
Posts: 30
Blog Entries: 1
Default Re: What is the purpose of toString()?

Friends sorry for confused you my point is that every class in java don't override toString() method eg- BufferedReader.
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the purpose of Constructor ? vbraojava Object Oriented Concepts 17 19 August 2011 12:12 AM
What is the purpose of Return Statement ? vbraojava Flow Control 2 06 July 2011 03:27 PM
What is the purpose of release() method of Tag ? vbraojava Servlets & Java Server Pages (JSP) 0 30 April 2009 04:46 PM
What is the purpose of <jsp:fallback> action tag ? vbraojava Servlets & Java Server Pages (JSP) 0 15 April 2009 11:51 AM
What is the purpose of Execution Engine in JVM ? vbraojava Java Language Basics/Fundamentals 4 28 September 2008 12:34 PM


All times are GMT +5.5. The time now is 01:48 AM.


Powered by vBulletin® Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright © 2008 ChetanaS, All Rights Reserved.