Importance of toString() in java

>> Saturday, October 20, 2012

Object to String using toString method_JavabynataraJ
Object class of Java is having predefined toString() method. This method by default Object class calls implicitly when an object created. Overriding toString manually is nothing but implementing this method in our class.The java toString() method is used when we need a string representation of an object. It is defined in Object class.

The toString() method is useful for debugging. By default, when an object is printed out in a print stream like System.out, the toString() method of the object is automatically called.
While develping the code the developers used to check the object properties are getting through the object or not. For this print statement will be useful to quick test in the console.

public String toString()
Returns: a string representation of the object.
Let us take a Person class and try to print the person object using the System.out.println() statement.

package blog.javabynataraj;
//@author Muralidhar N
class Person{
 public String fname;
 public String lname;
 
 Person(String fn,String ln){
  this.fname=fn;
  this.lname=ln;
 }
 public String getFname() {
  return fname;
 }
 public void setFname(String fname) {
  this.fname = fname;
 }
 public String getLname() {
  return lname;
 }
 public void setLname(String lname) {
  this.lname = lname;
 }
}

public class ToStringTest {
 public static void main(String[] args) {
  Person p = new Person("murali","dhar");
  System.out.println(p);
 }
}

The output will be the class name@hexadecimal

here the Object class's method toString returning
getClass().getName()+'@'+Integer.toHexString(hashCode())
But here we assumed that the firstname and the lastname will be print. But it is not done. This is the magic toStirng method.

But in the below program we can print the firstname and lastname of person Object. What we are going to do here is just overriding the toString method in Person Class.

package blog.javabynataraj;
//@author Muralidhar N
class Person{
 public String fname;
 public String lname;
 
 Person(String fn,String ln){
  this.fname=fn;
  this.lname=ln;
 }
 public String getFname() {
  return fname;
 }
 public void setFname(String fname) {
  this.fname = fname;
 }
 public String getLname() {
  return lname;
 }
 public void setLname(String lname) {
  this.lname = lname;
 }
 public String toString(){
  return (getClass()+"  FirstName: "+fname+"  LastName: "+lname);
 }
}

public class ToStringTest {
 public static void main(String[] args) {
  Person p = new Person("murali","dhar");
  System.out.println(p);
 }
}
See the output of this:
Now you can achieve this by overriding the default toString() method inside Person class to return the contents of the instance of Person.

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Google Bookmark Yahoo ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl

7 comments:

jojopig.com November 10, 2012 at 3:26 AM  

Thanks for the great read. Great Blog.

apkany December 3, 2012 at 6:05 AM  

thank you.. please give me information about the java programs with sourse code best website...

http://www.ecorptrainings.com/

kishore December 5, 2012 at 8:56 PM  

in this program we execute getClass() also then print output

class Person FirstName: murali LastName: Dhar


in this program getFname(),setFname(),getLname(),setLname() are used why these methods..these are not writing the program gives same output..what is the purpose behind that one...
tell me the answer please sir

dndInfotainment p December 26, 2012 at 11:21 PM  

Hi,
I am from Virat Bharat I read This Blog. This Is very Informative to our Youth. Really Good If you Want to see our Site You can easily Visit

http://www.viratbharat.com/

Thanking You
Puneet Kardam

trishna allen March 8, 2013 at 4:44 AM  

Gripping article. Excavation through and smashing luck with your very great activity. I care you make a expensiveness measure to contemplate using benefits articles can message. Thanks for intercourse. phoenix interior design | arizona interior design | scottsdale interior designer

Power Point Presentations April 27, 2013 at 7:32 AM  

Hello dears Bloggers Java Developers, Iam Alfonso Franco who possess records on business programming at college , and I need to share my project Glassfish Api Deployment with your. presentations

Post a Comment

Related Posts Plugin for WordPress, Blogger...

Random Posts

JavabynataraJ - Find me on Bloggers.com Find me on blorner.com Technology Blogs JavabynataraJ Backlinks Blogarama - The Blog Directory

  © Blogger template Webnolia by Ourblogtemplates.com 2009

Back to TOP