140

PHP has a var_dump() function which outputs the internal contents of an object, showing an object's type and content.

For example:

class Person {
  private $firstName;
  private $lastName;

  public function __construct($firstName, $lastName) {
    $this->firstName = $firstName;
    $this->lastName = $lastName;
  }
}

$person = new Person('Jon', 'Smith');
var_dump($person);

will output:

object(Person)#1 (2) {
  ["firstName:private"]=>
  string(3) "Jon"
  ["lastName:private"]=>
  string(5) "Smith"
}

What is the equivalent in Java that will do the same?

1
  • 1
    have you tried db4o? it is not specifically for this, but I think will work perfectly; Feb 23, 2015 at 18:49

11 Answers 11

72

It is not quite as baked-in in Java, so you don't get this for free. It is done with convention rather than language constructs. In all data transfer classes (and maybe even in all classes you write...), you should implement a sensible toString method. So here you need to override toString() in your Person class and return the desired state.

There are utilities available that help with writing a good toString method, or most IDEs have an automatic toString() writing shortcut.

2
31

In my experience, var_dump is typically used for debugging PHP in place of a step-though debugger. In Java, you can of course use your IDE's debugger to see a visual representation of an object's contents.

4
  • 11
    Yups, most PHP or Web Developer forget about debugging. Because they prefer to see the debug result in browser.
    – GusDeCooL
    Aug 4, 2013 at 18:54
  • 11
    You can't use your IDE's debugger if, for example, you are programming your JSP code in Adobe Experience Manager's browser-based editor. :(
    – Trevor
    Sep 24, 2014 at 22:16
  • 1
    Maybe we can use it to test behaviour of your program in live environment by printing the vardump in html format.
    – omjego
    Dec 18, 2018 at 2:07
  • Sometimes, even when dealing with java, it is regrettably not possible or too much hassle to set up debugging.
    – timwaagh
    Nov 3, 2022 at 15:09
19

I think that the best way to do It, is using google-gson (A Java library to convert JSON to Java objects and vice-versa)

Download It, add "jar" file to your project

HashMap<String, String> map = new HashMap<String, String>();

map.put("key_1", "Baku");
map.put("key_2", "Azerbaijan");
map.put("key_3", "Ali Mamedov");

Gson gson = new Gson();

System.out.println(gson.toJson(map));

Output:

{"key_3":"Ali Mamedov","key_2":"Azerbaijan","key_1":"Baku"}

You can convert any object (arrays, lists and etc) to JSON. I think, that It is the best analog of PHP's var_dump()

3
  • 4
    Interesting... do you know why it is not a static method?
    – mirelon
    Feb 26, 2014 at 10:36
  • 2
    You can use It like a static method: new Gson().toJson(...) without creating a new variables. Feb 27, 2016 at 20:47
  • Imo, it's better if you abstracted it into a custom utility class rather than use it directly, this gives you flexibility to manage third party dependencies Jun 4, 2020 at 13:31
17

Your alternatives are to override the toString() method of your object to output its contents in a way that you like, or to use reflection to inspect the object (in a way similar to what debuggers do).

The advantage of using reflection is that you won't need to modify your individual objects to be "analysable", but there is added complexity and if you need nested object support you'll have to write that.

This code will list the fields and their values for an Object "o"

Field[] fields = o.getClass().getDeclaredFields();
for (int i=0; i<fields.length; i++)
{
    System.out.println(fields[i].getName() + " - " + fields[i].get(o));
}
2
  • what's that Field you're using, imported from where?
    – RASMiranda
    Dec 11, 2013 at 21:53
  • 3
    java.lang.reflect.Field
    – Harry Lime
    Dec 19, 2013 at 11:53
8

The apache commons lang package provides such a class which can be used to build up a default toString() method using reflection to get the values of fields. Just have a look at this.

2
6

I like to use GSON because it's often already a dependency of the type of projects I'm working on:

public static String getDump(Object o) {
    return new GsonBuilder().setPrettyPrinting().create().toJson(o);
}

Or substitute GSON for any other JSON library you use.

3

I use Jestr with reasonable results.

0
2

I found this method to dump object, try this String dump(Object object)

1

Just to addup on the Field solution (the setAccessible) so that you can access private variable of an object:

public static void dd(Object obj) throws IllegalArgumentException, IllegalAccessException {
    Field[] fields = obj.getClass().getDeclaredFields();
    for (int i=0; i<fields.length; i++)
    {
        fields[i].setAccessible(true);
        System.out.println(fields[i].getName() + " - " + fields[i].get(obj));
    } 

}
0

I think something similar you could do is to create a simple method which prints the object you want to see. Something like this:

public static void dd(Object obj) { System.out.println(obj); }

It's not the same like var_dump(), but you can get an general idea of it, without the need to go to your debugger IDE.

-1

You XML serialization, and you should get a very neat representation even of deeply nested objects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.