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.

How to print javascript object example

A Java class is a set of properties and methods and it is an blueprint for an object. An object is created using class in java.

When issue occurs during the development of a project, it is essential to print the java object to the console in order to examine the actual values for further debugging. By default, If you print an object using System.out.println or logger statements, It displays format packagename.classname@hashcode

If User.java class is declared in com.mycompany,

package com.mycompany;
public class User {
 private String name;

 public String getName(){
   return name;
 }
 pblic void setName(String name){
   this.name=name
 }
}

If you print the object of above class, The output as follows

com.mycompany.User@8e61f1a2

This isnā€™t going to help with object values debugging.

How do print the java object content without com.mycompany.User@8e61f1a2?

This post covers the multiple ways to display java object content.

  • Java.lang.Object toString() method

How to print Java object using toString() method

Java.lang.Object is the base class for all other classes in the java language, So each java class extends by default this class.

toString() is a method in java.lang.object class that is available in all java classes by default and that you can override in custom classes.

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}  

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.