Page 1 of 1

Basics of toString() Rate Topic: -----

#1 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Posted 12 January 2011 - 07:31 AM

*
POPULAR

toString() In Java

The toString() method solves so many question that we see these days in the forums, and it is crucially important to understand it. It is a method that is defined in the Object class and thus, every object has one, however, it prints out, for the most part, meaningless information. Let me show you an example. I’m going to write a simple class that stores the name and age of somebody:

public class Person {
	
	private int age; 
	private String name;
	
	public Person() {
		this (0, "");
	}

	
	public Person(int i, String string) {
		age = i;
		name = string;
	}


	public static void main(String[] args) {
		Person p = new Person(40, "John Doe");
		System.out.println(p);
	}
}



This program prints p as Person@3e25a5 which is the default implementation of toString(). This output is the class name (Person) and the hash code that represents that object. This is because under the covers System.out.println() calls an object’s toString() method implicitly.
However, this output is virtually meaningless. No end user wants to know what the class name or the hash code is. They want to know what the object IS. In that case, they will want to know the age and name of the Person object that they have. You only have to make one change – add a toString() method to whatever class that you want to describe. Following is an improved version of the above program with a toString() method added:

public class Person {
	
	private int age; 
	private String name;
	
	public Person() {
		this (0, "");
	}

	
	public Person(int i, String string) {
		age = i;
		name = string;
	}
	
	@Override
	public String toString() {
		return "Name: " + name + " \nAge: " + age + "\n\n";
	}


	public static void main(String[] args) {
		Person p = new Person(40, "John Doe");
		System.out.println(p);
	}
}



The output here is much nicer. This time it nicely tells the Name and the Age of the Person:
Name: John Doe
Age: 40






So, what uses does this have? Loads. If you EVER need a String representation of any object for any purpose, toString() is the easiest way to go. It can perform any number of computations – as long as it returns a String in the end. This makes it easier for objects that have arrays to return their values. It also makes debugging a lot easier and makes your objects easier to understand. Remember that if you ever need the old implementation with the addition of something new, you can always add super.toString() to the returned String.

Thanks for reading this short (yet important) tutorial, and I hope that you learned something!


Is This A Good Question/Topic? 11
  • +

Replies To: Basics of toString()

#2 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Posted 12 January 2011 - 07:58 AM

You posted this twice, notice the thread IDs.
Was This Post Helpful? 0
  • +
  • -

#3 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Posted 12 January 2011 - 08:04 AM

View PostSergio Tapia, on 12 January 2011 - 09:58 AM, said:

You posted this twice, notice the thread IDs.


I know. I reported the topic. It just hasn't been gotten to yet.
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3120
  • View blog
  • Posts: 19,165
  • Joined: 14-September 07

Posted 12 January 2011 - 08:11 AM

Taken care of.
Was This Post Helpful? 2
  • +
  • -

#5 Kutlar  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 29
  • Joined: 04-March 10

Posted 12 January 2011 - 05:31 PM

Not trying to be a smartie pants or anything, but shouldn't we always use the @Override annotation when writing our own toString() methods to keep from silly mistakes like not using the camel case when typing toString so our compiler knows that we're replacing the class's exisiting toString() method and not writing a tostring() :).
Good Stuff though :)
Was This Post Helpful? 3
  • +
  • -

#6 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Posted 12 January 2011 - 05:44 PM

While not required, it certainly is a good idea. Thanks for the feedback.
Was This Post Helpful? 1
  • +
  • -

#7 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 465
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Posted 18 January 2011 - 01:03 PM

Excuse me Dogstopper but are you overriding the toString() function? If every single object in Java have one in your example (the second code) are you overriding it?
EDIT:
Heheh i dont read the above post :D

This post has been edited by NoBrain: 18 January 2011 - 01:09 PM

Was This Post Helpful? 0
  • +
  • -

#8 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Posted 18 January 2011 - 01:09 PM

Yes, that is the case. The Object class defines it, and EVERY object extends the Object class. Thus, it is in fact, an override.
Was This Post Helpful? 0
  • +
  • -

#9 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2793
  • View blog
  • Posts: 15,904
  • Joined: 24-September 08

Posted 19 January 2011 - 03:57 PM

I'm sorry, I absolutely mean to be a smart ass, but aren't you missing your @Override annotation? :D
Was This Post Helpful? 0
  • +
  • -

#10 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Posted 19 January 2011 - 04:33 PM

Nope. Don't think I am.
Was This Post Helpful? 0
  • +
  • -

#11 irenicus09  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 26-January 11

Posted 15 April 2011 - 10:23 AM

View PostNeoTifa, on 19 January 2011 - 03:57 PM, said:

I'm sorry, I absolutely mean to be a smart ass, but aren't you missing your @Override annotation? :D


Lol, was funny. Anyway, nice tutorial there!

This post has been edited by irenicus09: 15 April 2011 - 10:24 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1