在创建程序时,经常需要找出对象的内容。为此,Java Object类(类层次结构中最基本的 Java 类)定义了一个特殊的toString()方法。它返回描述该对象的字符串。Java 中toString()方法的默认实现产生如下输出:
package.class@hashCode
即java.lang.Object中定义的toString以十六进制形式给出对象的哈希码。这通常不是很清楚和有用。所以在创建新类时,习惯上重写toString(),以便返回字符串包含类名、所有变量的名称和值。正确重写toString方法可以帮助记录和调试 Java 程序,它提供了有价值且重要的信息。简单来说,Java中使用toString()方法来获取表示数字对象值的字符串对象。换句话说,它将数字转换为字符串。如果该方法采用简单数据类型作为参数,则返回一个表示简单数据类型值的字符串对象。
Java toString() 方法示例
通常,如果您创建一个类,除了Java 中的toString()方法之外,您还需要重写equals、hashCode和CompareTo方法,以便了解如何使用其对象。但现在,让我们关注toString。在大多数情况下,提交有关对象的文本信息非常简单。您只需要了解用户在登录时到底想看到什么。让我们举一个例子并创建一个描述平面上的点的类。它有两个坐标:坐标X、坐标Y。当然,如果用户询问“这是什么对象”,它们应该显示在屏幕上,就像类本身的名称一样。这就是我们将在重写的toString()方法中演示的内容。现在我们创建类本身、它的字段(坐标)、构造函数、用于获取坐标的 getter(当然,可以不创建它们,但从正确使用 OOP 的角度来看,getter 是很好的做法)。为了说明这一点,我们还创建一个方法来创建一个新点 - 其他两个点的总和,并重写 equals 和hashCode方法。public class Point implements Comparable<Point> {
private final int coordinateX;
private final int coordinateY;
public Point(final int coordinateX, final int coordinateY) {
this.coordinateX = coordinateX;
this.coordinateY = coordinateY;
}
public int getX() {
return coordinateX;
}
public int getY() {
return coordinateY;
}
public Point sum(final Point that) {
return new Point(coordinateX + that.coordinateX, coordinateY + that.coordinateY);
}
// here we override toString with coordinates and class name
@Override
public String toString() {
return "Point{"
+ "X: " + getX()
+ ", Y: " + getY()
+ '}';
}
@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (o == null || o.getClass() != this.getClass()) {
return false;
}
@Override
public int hashCode() {
return coordinateX + coordinateY;
}
Now let’s create a testing class and a main method. There we’ve got two points and the third one, that we’re getting from the summation of two points. We apply toString method to this point and print it out.
//toString() method demo
public class Test {
public static void main(String[] args) {
Point myPoint1 = new Point(5,7);
Point myPoint2 = new Point(-2, 8);
Point mySum = myPoint1.sum(myPoint2);
//call toString explicitly
System.out.println(mySum.toString());
}
}
The result of this program work is below:
Point{X: 3, Y: 15}
You don't actually need to explicitly call the toString method, and most often don't. To call the toString() method, simply pass the desired object to System.out.println. So our test class will look like this:
//toString method demo
public class Test {
public static void main(String[] args) {
Point myPoint1 = new Point(5,7);
Point myPoint2 = new Point(-2, 8);
Point mySum = myPoint1.sum(myPoint2);
//call toString implicitly
System.out.println(mySum);
}
}
The output of the program is the same as before. What if we don't override the toString method, but want to print the object? In this case, the toString method of the Object class will be called. In our example, if we remove the toString method from our Point class, and leave everything as it is in the test class, we will get the following result:
Point@12
Here we’ve got the hash code of the object in hexadecimal.