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) {  
  this.name=name;  
 }  
 public Integer getId() {  
  return id;  
 }  
 public void setId(Integer id) {  
  this.id = id;  
 }  
 public String getName() {  
  return name;  
 }  
 public void setName(String name) {  
  this.name = name;  
 }  
  
}  
Employee emp=new Employee();  
System.out.println(emp);  
System.out.printl/n(emp.toString());  

Output is

Employee@1517365b  
Employee@1517365b  

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 {  
  
 -----------------  
@Override  
 public String toString() {  
  return "Employee("+name+")";  
 }  
}  
Employee emp=new Employee();  
 emp.setName("kiran");  
 System.out.println(emp);  
 System.out.println(emp.toString());  

Output is

kiran  
kiran  

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(Arrays.toString(emps));  
  
  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");  
  System.out.println(Arrays.deepToString(emps1));  
  
 }  
  
}  

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(empList);  
  
  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(empSet);  
  
  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"));  
  System.out.println(empMap);  
 }  
  
}

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 {  
  
---------------  
 @Override  
  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");  
  System.out.println(emp.toString());  
  System.out.println(emp.toJson());  

Output

Employee[name=Kiran,id=1]  
  {"name": "Kiran", "id": 1}  

Similar Posts