Sunday, September 30, 2018

Different ways to print java object tutorials with examples

In this blog post, We are going to learn the Print object content as String with examples.
java print object example

Java Object Content Print

Java object holds properties and methods. The object can be Collections of Custom classes. When any issues come in project development, There is a need to print the java object to console to inspect the actual values for more debugging

Each class in java extends Java.lang.Object and toString() methods
if you call any object with toString() method, It just gives the name of the class + @ symbol + hashcode of that class, We will not get any useful information with this to inspect the data of an object. By Defaults, When you print an object or object.toString() method, It always gives the class the with the hash code as below.

This class not provided toString() method implementation. It calls the java.lang.object.toString() behaviour.
public class Employee {

 private Integer id;
 private String name;
 public Employee(String name) {;
 public Integer getId() {
  return id;
 public void setId(Integer id) { = id;
 public String getName() {
  return name;
 public void setName(String name) { = name;

Employee emp=new Employee();
Output is

Print object content as String using custom toString() method. 

Overridden toString() method to include all the fields that you want to print to console. This below example explains
  • How to override toString() method to print object - toString() method 
  • How to print java object as a String object
public class Employee {

 public String toString() {
  return "Employee("+name+")";
Employee emp=new Employee();
Output is

Java array objects print example 

If there are an array of multiple objects, Need to print all the objects. It looks not good to iterate each object and print for just debugging purpose.
The below example explains
  • How to print a single array of objects - Arrays.toString() method 
  • How to print a multidimensional array of objects Arrays.deepToString() method
Arrays.toString() method - returns string representation of each object in array Arrays.deepToString() method - returns a string representation of each object in the multidimensional array - Array elements contain an array of elements
import java.util.Arrays;
public class EmployeeTest {

 public static void main(String[] args) {
  Employee[] emps = { new Employee("Kiran"), new Employee("John") };
  System.out.println("java Array of  Object print example");

  System.out.println("java multi dimensional Array of Objects print example");
  Employee[][] emps1 = new Employee[2][2];
  emps1[0][0] = new Employee("one");
  emps1[0][1] = new Employee("two");
  emps1[1][0] = new Employee("three");
  emps1[1][1] = new Employee("foour");


Output is
java Array of  Object print example
[Employee(Kiran), Employee(John)]
java multi dimensional Array of Objects print example
[[Employee(one), Employee(two)], [Employee(three), Employee(foour)]]

Collection of objects print example

The collection can be set, Map and List. When you are printing collection object to console, It will call toString() on each element.
This below example explains about
  • How to print ArrayList of objects to console 
  • How to print Set of objects to console 
  • How to print Map of objects to console
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class EmployeeTest {

 public static void main(String[] args) {

  System.out.println("java ArrayList of Objects print example");
  List empList = new ArrayList<>();
  empList.add(new Employee("one"));
  empList.add(new Employee("two"));

  System.out.println("java Set of Objects print example");
  Set empSet = new HashSet<>();
  empSet.add(new Employee("three"));
  empSet.add(new Employee("four"));

  System.out.println("java Map of Objects print example");
  Map empMap = new HashMap();
  empMap.put(1, new Employee("five"));
  empMap.put(2, new Employee("six"));

output is
java ArrayList of Objects print example
[Employee(one), Employee(two)]
java Set of Objects print example
[Employee(three), Employee(four)]
java Map of Objects print example
{1=Employee(five), 2=Employee(six)}

ToStringBuilder class - Print all fields of a java object as a string 

ToStringBuilder class is defined apache commons library.
ToStringBuilder.reflectionToString method prints all fields of a java object to  String  using ToStringStyle.SHORT_PREFIX_STYLE, to JSon using
ToStringStyle.JSON_STYLE class.
These below examples explain about
  • How to print java object all fields as a string - toStringStyle.SHORT_PREFIX_STYLE
  • How to print java object as JSON string - ToStringStyle.JSON_STYLE
public class Employee {

  public String toString () {
     return ToStringBuilder.reflectionToString(this,ToStringStyle.SHORT_PREFIX_STYLE);
  public String toJson () {
     return ToStringBuilder.reflectionToString(this,ToStringStyle.JSON_STYLE);

Employee emp = new Employee("one");
  {"name": "Kiran", "id": 1}

Related article