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.

Dani AI

Generated

As and noted, the JDK provides two different behaviors here; this expands on the why and gives quick fixes.

The default Object.toString() produces a machine-oriented identifier (class name + '@' + hex of hashCode()); see the Java docs for Object (java.lang.Object). Most concrete collection classes supply their own toString() that iterates the contents and calls each element's toString(), so the collection prints readable element representations rather than the raw identity string. Maps format entries as key=value pairs and collections typically use [...].

If the elements themselves never override toString(), the collection will show those elements using the default identity string — you’ll still see ClassName@hex for each element. To produce readable output implement toString() in your domain classes. Example:

public class Person {
  private String name;
  private int age;

  @Override
  public String toString() {
    return name + " (" + age + ")";
  }
}

Printing a List<Person> will then show entries like [Alice (30), Bob (25)].

Troubleshooting notes: arrays do not override toString() (use Arrays.toString / Arrays.deepToString — see java.util.Arrays); if you need the JVM identity hash regardless of any overridden hashCode() use System.identityHashCode (java.lang.System). Do not expose sensitive data in toString(). For larger objects consider IDE-generated toString() or a builder utility to keep output consistent and easy to read.

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.