How do I print my Java object without getting "SomeType@2f92e0f4"?
To print a Java object in a more readable format, you can override the toString()
method in your class. The toString()
method is called when an object is passed to the System.out.println()
method, or when an object is concatenated with a string using the +
operator.
Here's an example of how you can override the toString()
method to print a more readable representation of your object:
public class SomeType {
private int x;
private int y;
public SomeType(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return "SomeType{x=" + x + ", y=" + y + "}";
}
}
SomeType obj = new SomeType(1, 2);
System.out.println(obj); // prints "SomeType{x=1, y=2}"
I hope this helps! Let me know if you have any questions.