Reputation: 1473
When I print the instances of some classes (e.g. ArrayList
) to a stream, e.g. System.out.println(
instance of ArrayList)
, it doesn't print the reference id (e.g. ArrayList@2144c53d), but the actual values, with some formatting (e.g. [1,2,3,4]). I was wondering how I can do this for my own classes? Do I perhaps have to define some method/implement some interface?
Upvotes: 4
Views: 2538
Reputation: 13066
When you try to print an object of a class using System.out.print
, it looks for toString()
method in that class. If it doesn't find in the given class then it tries to look up that method in its superclass untill it finds one. And then print the String
returned by toString()
method defined in the class. And if a class doesn't extend anything then it by default calls the toString
method of Object
, which returns the String something like this:
public String toString()
{
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
That's why you are getting such type of output when printing your own class object. To print different you should override toString()
method in your own class . For example:
public class MyClass
{
private int roll;
private String name;
MyClass(int roll,String name)
{
this.roll = roll;
this.name = name;
}
@Override
public String toString()
{
return "roll="+roll+",name="+name;
}
}
Upvotes: 4
Reputation: 48592
Override
toString()
method.
This method is Object class which basically give output like.
getClass().getName() + '@' + Integer.toHexString(hashCode())
To get anything else you need to override this in your class.
Example: ArrayList when you print arraylist the its give the arraylist elements intead of ArrayList@2144c53d
public String toString() {
return "Stackoverflow";
}
Upvotes: 1
Reputation: 46408
. I was wondering how I can do this for my own classes?
By Overriding toString()
method in your class.
public class MyClass {
@override
public String toString(){
return "I am a legend MwhAhAH";
}
}
If you don't override toStirng() in your class, Object#toString()
will be invoked, which returns yourclassname+ "@"+ hexnumberOfHashcode
.
public String toString() {
237 return getClass().getName() + "@" + Integer.toHexString(hashCode());
238 }
ArrayList does override toString method:
public String toString() {
431 Iterator<E> i = iterator();
432 if (! i.hasNext())
433 return "[]";
434
435 StringBuilder sb = new StringBuilder();
436 sb.append('[');
437 for (;;) {
438 E e = i.next();
439 sb.append(e == this ? "(this Collection)" : e);
440 if (! i.hasNext())
441 return sb.append(']').toString();
442 sb.append(", ");
443 }
444 }
445
446}
Upvotes: 2
Reputation: 12010
Simple, you should just need to override toString()
method.
Something like the following will need to be done:
public class MyClass {
@override
public String toString(){
return "My custom string, perhaps using some of the class propertis";
}
}
Upvotes: 0
Reputation: 1500235
Simple: you override the Object.toString()
method. For example:
public class Person {
private final String name;
private final LocalDate birthDate;
public Person(String name, LocalDate birthDate) {
this.name = name;
this.birthDate = birthDate;
}
@Override public String toString() {
return String.format("%s (born %s)", name, birthDate);
}
}
For more complex handling, you might want to consider implementing the Formattable
interface - although I've never personally done so myself.
Upvotes: 11