Print String representation of an object in Java
This post will discuss how to print the string representation of an object in Java.
In the above post, we have discussed how to override the toString() method in Java to print the string representation of an object. This post provides an overview of some of the other alternatives to accomplish this without need to override the toString() method.
1. Using Apache Commons Lang
Apache Commons Lang library provides the ToStringBuilder.reflectionToString() method that uses reflection to generate a string representation for the specified object.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import org.apache.commons.lang3.builder.ToStringBuilder; class Country { private String name; private String continent; private int population; public Country(String name, String continent, int population) { this.name = name; this.continent = continent; this.population = population; } } public class Main { public static void main(String[] args) { Country country = new Country("United States", "North America", 4000000); System.out.println(ToStringBuilder.reflectionToString(country)); } } |
Output:
Country@5c29bfd[continent=North America,name=United States,population=4000000]
Note that this method might fail under a security manager, since it uses AccessibleObject.setAccessible() to change the visibility of the private fields in the class. Alternatively, you can also use the ReflectionToStringBuilder.toString() method which also uses reflection to access private fields.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import org.apache.commons.lang3.builder.ReflectionToStringBuilder; class Country { private String name; private String continent; private int population; public Country(String name, String continent, int population) { this.name = name; this.continent = continent; this.population = population; } } public class Main { public static void main(String[] args) { Country country = new Country("United States", "North America", 4000000); System.out.println(ReflectionToStringBuilder.toString(country)); } } |
Output:
Country@5c29bfd[continent=North America,name=United States,population=4000000]
2. Using Project Lombok
With Project Lombok, you can annotate any class with @ToString annotation, to get an implementation of the toString() method consisting of your class name with each field, separated by commas, unless specified otherwise.
The default implementation prints all non-static fields. You can annotate a field with @ToString.Exclude to skip it. Alternatively, you can specify the exact fields to be included in the string representation with @ToString.Include and annotate the class with @ToString(onlyExplicitlyIncluded = true).
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import lombok.ToString; @ToString class Country { private String name; private String continent; private int population; public Country(String name, String continent, int population) { this.name = name; this.continent = continent; this.population = population; } } |
3. Using GSON
Alternatively, you can use GSON library to serialize a Java object into JSON string. Gson provide toJson() method to convert Java objects to JSON, which will perform a deep copy using reflection.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import com.google.gson.Gson; import com.google.gson.GsonBuilder; class Country { private String name; private String continent; private int population; public Country(String name, String continent, int population) { this.name = name; this.continent = continent; this.population = population; } } public class Main { private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create(); public static void main(String[] args) { Country country = new Country("United States", "North America", 4000000); System.out.println(GSON.toJson(country)); } } |
Output:
{
"name": "United States",
"continent": "North America",
"population": 4000000
}
4. Using Jackson
If you prefer the Jackson library, you can use ObjectMapper#writeValueAsString() method to serialize a Java Object to JSON.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
import org.codehaus.jackson.map.ObjectMapper; import java.io.IOException; class Country { private String name; private String continent; private int population; public Country(String name, String continent, int population) { this.name = name; this.continent = continent; this.population = population; } public String getName() { return name; } public String getContinent() { return continent; } public int getPopulation() { return population; } } public class Main { public static void main(String[] args) throws IOException { Country country = new Country("United States", "North America", 4000000); ObjectMapper mapper = new ObjectMapper(); String jsonString = mapper.writeValueAsString(country); System.out.println(jsonString); } } |
Output:
{"name":"United States","continent":"North America","population":4000000}
That’s all about printing the String representation of an object in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)