Java Object toString method is one of the basics in Java.
toString function returns a string representation of an object.
I’ll explain how to add toString method to a Java class with code examples.
Using toString in Java
To string method in Java should be informative to make it easy to read.
it’s recommended to override this method in each class.
By default Java object toString method implementation looks like this:
getClass().getName() + "@" + Integer.toHexString(hashCode())
For example, you have a Car class:
package com.explainjava;
public class Car {
private String name;
private int year;
public Car(String name, int year) {
this.name = name;
this.year = year;
}
}
The output of default toString method will be the following:
com.explainjava.Car@1324409e
It’s not human readable.
That’s why we need to override it.
I’ll provide you 3 ways how to do it without any problems.
Generate toString using Intellij IDEA
There is “Generate” option in the main menu.
It proposes you to generate few boilerplate things like a toString() method:
Select toString method and choose class fields to use:
The result is:
package com.explainjava;
public class Car {
private String name;
private int year;
public Car(String name, int year) {
this.name = name;
this.year = year;
}
@Override
public String toString() {
return "Car{" +
"name='" + name + '\'' +
", year=" + year +
'}';
}
}
If you’ll print a car object like this:
public static void main(String[] args) {
System.out.println(new Car("BMW", 2017));
}
Output:
Car{name='BMW', year=2017}
The result is good, but it’s not convenient to change toString method every time when you want to add a new class field.
ToStringBuilder from apache.commons
If you want to print all fields every time and don’t want to change toString method every time – ToStringBuilder is a good choice.
First of all, you need to add maven dependency to the commons-lang3 library:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.6</version>
</dependency>
ToString method looks like this:
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
}
Output:
com.explainjava.Car@7ac7a4e4[
name=BMW
year=2017
]
As you can see 2nd parameter is a style.
There are 7 options, so you’ll definitely find a good style for you.
Lombok @ToString
It’s my favorite way to make toString method in Java.
Lombok is awesome production-ready Java library to generate your boilerplate code.
Installation guide is here.
Example:
package com.explainjava;
import lombok.AllArgsConstructor;
import lombok.ToString;
@ToString
@AllArgsConstructor
public class Car {
private String name;
private int year;
}
Looks better? No, looks excellent!
Output:
Car(name=BMW, year=2017)
As you can see Lombok can generate constructors as well and more “tasty” things.
You should at least take a look at its features.
Java Array ToString Method
If you’ll try to print an array like this:
public static void main(String[] args) {
Car[] cars = new Car[] { new Car("BMW", 2017), new Car("Audi", 2017) };
System.out.println(cars);
}
You’ll get a little bit, not human-readable output:
[Lcom.explainjava.Car;@1324409e
The first idea is to iterate through the array and print each element like this:
public static void main(String[] args) {
Car[] cars = new Car[] { new Car("BMW", 2017), new Car("Audi", 2017) };
for (Car car : cars) {
System.out.println(car);
}
}
But there is a better solution in Java:
public static void main(String[] args) {
Car[] cars = new Car[] { new Car("BMW", 2017), new Car("Audi", 2017) };
System.out.println(Arrays.toString(cars));
}
Output:
[Car(name=BMW, year=2017), Car(name=Audi, year=2017)]
Conclusion
ToString method in Java looks easy, but there are many ways how to make your life easier and generate boilerplate code.
Don’t afraid to use Lombok, it’s awesome.
Please, ask questions.