string - How do I print my Java object without getting "SomeType@2f92e0f4"? -
i have class defined follows:
public class person { private string name; // constructor , getter/setter omitted }
i tried print instance of class:
system.out.println(myperson);
but got following output: com.foo.person@2f92e0f4
. similar thing happened when tried print array of person
objects:
person[] people = //... system.out.println(people);
i got output: [lcom.foo.person;@28a418fc
what output mean? how change output contains name of person? , how print collections of objects?
note: intended canonical q&a subject.
background
all java objects have tostring()
method, invoked when try , print object.
system.out.println(myobject); // invokes myobject.tostring()
this method defined in object
class (the superclass of java objects). object.tostring()
method returns ugly looking string, composed of name of class, @
symbol , hashcode of object in hexadecimal. code looks like:
// code of object.tostring() public string tostring() { return getclass().getname() + "@" + integer.tohexstring(hashcode()); }
a result such com.foo.mytype@2f92e0f4
can therefore explained as:
com.foo.mytype
- name of class, i.e. classmytype
in packagecom.foo
.@
- joins string together2f92e0f4
hashcode of object.
the name of array classes little different, explained in javadocs class.getname()
. instance, [ljava.lang.string
means:
[
- single-dimensional array (as opposed[[
or[[[
etc.)l
- array contains class or interfacejava.lang.string
- type of objects in array
customizing output
to print different when call system.out.println(myobject)
, must override tostring()
method in own class. here's simple example:
public class person { private string name; // constructors , other methods omitted @override public string tostring() { return name; } }
now if print person
, see name rather com.foo.person@12345678
.
bear in mind tostring()
one way object converted string. typically output should describe object in clear , concise manner. better tostring()
our person
class might be:
@override public string tostring() { return getclass().getsimplename() + "[name=" + name + "]"; }
which print, e.g., person[name=henry]
. that's useful piece of data debugging/testing.
if want focus on 1 aspect of object or include lot of jazzy formatting, might better define separate method instead, e.g. string toelegantreport() {...}
.
auto-generating output
many ides offer support auto-generating tostring()
method, based on fields in class. see docs eclipse , intellij, example.
several popular java libraries offer feature well. examples include:
@tostring
annotation project lombok
printing groups of objects
so you've created nice tostring()
class. happens if class placed array or collection?
arrays
if have array of objects, can call arrays.tostring()
produce simple representation of contents of array. instance, consider array of person
objects:
person[] people = { new person("fred"), new person("mike") }; system.out.println(arrays.tostring(people)); // prints: [fred, mike]
note: call static method called tostring()
in arrays class, different we've been discussing above.
if have multi-dimensional array, can use arrays.deeptostring()
achieve same sort of output.
collections
most collections produce pretty output based on calling .tostring()
on every element.
list<person> people = new arraylist<>(); people.add(new person("alice")); people.add(new person("bob")); system.out.println(people); // prints [alice, bob]
so need ensure list elements define nice tostring()
discussed above.
Comments
Post a Comment