If a class doesnt override toString() method and tries to print out the object like this:
//some class

public class SomeClass1{

	int x=4;
}
//another class
class DemoOftoString 
{
	public static void main(String[] args) 
	{
		SomeClass1 o=new SomeClass1();
		System.out.println(o);
	}
}

output:SomeClass1@hex version of object's hash code

------------------------------------------
But when a collection object is called with collectionobject.toString() it returns the values...but it doesnt return classname@hex value of objects hash code..instead it return values.....
Can anyone help with this....thanks
regards,
Fardoon.

Recommended Answers

All 3 Replies

The toString method is inherited from a parent class if it isn't overriden in the class that you are calling on. This is true for all classes, since all classes derive from at least the Object class, which contains a toString method. The Object.toString() method prints the name of the class, followed by the @ symbol, followed by the object's hash code, so that is what you are seeing when you print your SomeClass1 object. I suspect that your collectionobject class either contains a toString method or derives from a class that does.

All Collections framework classes implement toString() to return a list of all elements in the Collection, calling the toString() of each element.

Thanks for clearing my doubt.....cheers
Regards,
Fardoon

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.