How can I output data for a Java object that is not the same value as "SomeType@2f92e0f4"?

Asked 1 years ago, Updated 1 years ago, 136 views

You have defined the following classes:

public class Person {
  private String name;

  // // constructor and getter/setter omitted
}

Attempted to print an object for this Person class.

System.out.println(myPerson);

Output value: com.foo.Person@2f92e0f4

Similar results were output when outputting an array with objects from the Person class.

Person[] people = //...
System.out.println(people); 

Output value: [Lcom.foo.Person;@28a418fc

What is the meaning of the output value? Person How do I modify a class object to have a value in the name field? And how do I output the value of an array of objects?

object java tostring string

2022-09-22 21:33

1 Answers

All objects in Java have the toString() method. It automatically invokes the method when you want to print out the value of the object.

System.out.println(myObject);  // invokes myObject.toString()

This method is defined by the [Object] (http://docs.oracle.com/javase/8/docs/api/java/lang/Object.html) class, which is a superclass of all classes in Java. The Object.toString() method returns an odd-looking string. The strange string consists of the class's name, @ character, and the object's hashcode represented in hexadecimal. The toString() method of the Object class is defined as follows:

// Code of Object.toString()
public String toString() {
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

Therefore, the output value com.foo.MyType@2f92e0f4 has the following meanings.

As well described in Javadoc's Class.getName(), the array class name is represented slightly differently. For example, [Ljava.lang.String means:

The 'toString() method must be override within the class to output differently when calling System.out.println(myObject). Here's a quick example:

public class Person {

  private String name;

  // // constructors and other methods omitted

  @Override
  public String toString() {
    return name;
  }
}

When you output an object in the Person class, the value of the name field for that object will then be displayed instead of com.foo.Person@12345678.

Remember that toString() is one way to convert an object to a string. Typically, this output should be able to describe the entire data of the object in a clear and concise manner. Therefore, it is better to define the toString() method of the Person class as follows:

@Override
public String toString() {
  return getClass().getSimpleName() + "[name=" + name + "]";
}

Examples of output values: Person[name=Henry] This value can be very useful when debugging or testing.

If you want to focus on one aspect of the object's data or specify a more colorful format, you may want to define another method (for example, String toElegantReport()') that is distinct from the toString() method.

Many IDE provide the ability to automatically generate toString() methods based on the fields defined in that class. Check out IDE's documents such as Eclipse and IntelliJ.

Several famous Java libraries also offer this feature. For example:

You can now define toString() well when defining a class. So how do we deal with the classes in the array or collection?

Arrangement

For an array of objects, you can invoke the Arrays.toString() method to simply represent the data in the array. For example, consider an array of Person objects:

Person[] people = { new Person("Fred"), new Person("Mike") };
System.out.println(Arrays.toString(people));

// // Prints: [Fred, Mike]

Note: This is not the toString() of the Object class we've been discussing, but the static method called toString() of the Array class.

If you are using a multidimensional array, you can use the Arrays.deepToString() method to obtain the same output value as above.

Collection

Most collections can generate a good-looking output using the .toString() method for all elements.

As described above, the toString() method needs to be well defined for each element in the collection.


2022-09-22 21:33

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.