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. class mytype in package com.foo.
  • @ - joins string together
  • 2f92e0f4 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 interface
  • java.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:


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

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -