What is the Java equivalent of PHP var_dump?
JavaPhpJava Problem Overview
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?
Java Solutions
Solution 1 - Java
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.
Solution 2 - Java
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.
Solution 3 - Java
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));
}
Solution 4 - Java
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()
Solution 5 - Java
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.
Solution 6 - Java
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.
Solution 7 - Java
I use [Jestr][1] with reasonable results.
[1]: http://sourceforge.net/projects/jestr "Jestr"
Solution 8 - Java
I found this method to dump object, try this String dump(Object object)
Solution 9 - Java
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.
Solution 10 - Java
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));
}
}
Solution 11 - Java
You XML serialization, and you should get a very neat representation even of deeply nested objects.