It may optionally have uses within the context of an application but far more often it is used for debugging purposes. For example, when you hit a breakpoint in an IDE, it's far easier to read a meaningful toString() of objects than it is to inspect their members.
对于 toString()方法应该做什么没有设定要求。按照约定,它通常会告诉您类的名称和相关数据成员的值。通常,toString()方法是在 IDE 中自动生成的。
Relying on particular output from a toString() method or parsing it within a program is a bad idea. Whatever you do, don't go down that route.
类的字符串表示形式
一般来说,toString
方法返回一个字符串,该字符串
“文本表示”这个对象。
The result should be a concise but
信息表示,即
对一个人来说很容易阅读。它是
recommended that all subclasses
override this method.
类 Object的 toString方法
返回一个字符串,该字符串由
对象的类的名称
is an instance, the at-sign character
“@”和无符号十六进制
类的哈希代码的表示形式
换句话说,这个方法
返回与值相等的字符串
以下人士:
The main purpose of toString is to generate a String representation of an object, means the return value is always a String. In most cases this simply is the object's class and package name, but on some cases like StringBuilder you will got actually a String-text.
无论何时您需要研究 String表单中名为 value 的构造函数,您都可以简单地使用 String.toString..。
举个例子。
package pack1;
import java.util.*;
class Bank {
String n;
String add;
int an;
int bal;
int dep;
public Bank(String n, String add, int an, int bal) {
this.add = add;
this.bal = bal;
this.an = an;
this.n = n;
}
public String toString() {
return "Name of the customer.:" + this.n + ",, "
+ "Address of the customer.:" + this.add + ",, " + "A/c no..:"
+ this.an + ",, " + "Balance in A/c..:" + this.bal;
}
}
public class Demo2 {
public static void main(String[] args) {
List<Bank> l = new LinkedList<Bank>();
Bank b1 = new Bank("naseem1", "Darbhanga,bihar", 123, 1000);
Bank b2 = new Bank("naseem2", "patna,bihar", 124, 1500);
Bank b3 = new Bank("naseem3", "madhubani,bihar", 125, 1600);
Bank b4 = new Bank("naseem4", "samastipur,bihar", 126, 1700);
Bank b5 = new Bank("naseem5", "muzafferpur,bihar", 127, 1800);
l.add(b1);
l.add(b2);
l.add(b3);
l.add(b4);
l.add(b5);
Iterator<Bank> i = l.iterator();
while (i.hasNext()) {
System.out.println(i.next());
}
}
}
public final class Coordinates {
private final double x;
private final double y;
public Coordinates(double x, double y) {
this.x = x;
this.y = y;
}
public static void main(String[] args) {
Coordinates coordinates = new Coordinates(1, 2);
System.out.println("Bourne's current location - " + coordinates);
}
}
指纹
Bourne's current location - Coordinates@addbf1 //concise, but not really useful to the reader
现在,重写坐标类中的 toString () ,如下所示,
@Override
public String toString() {
return "(" + x + ", " + y + ")";
}
results in
Bourne's current location - (1.0, 2.0) //concise and informative
当对包含对这些对象的引用的集合调用该方法时,重写 toString ()的用处就更大了。例如,以下
public static void main(String[] args) {
Coordinates bourneLocation = new Coordinates(90, 0);
Coordinates bondLocation = new Coordinates(45, 90);
Map<String, Coordinates> locations = new HashMap<String, Coordinates>();
locations.put("Jason Bourne", bourneLocation);
locations.put("James Bond", bondLocation);
System.out.println(locations);
}
prints
{James Bond=(45.0, 90.0), Jason Bourne=(90.0, 0.0)}
而不是这样,
{James Bond=Coordinates@addbf1, Jason Bourne=Coordinates@42e816}
几个实现指南,
You should almost always override the toString() method. One of the cases where the override wouldn't be required is utility classes that group static utility methods, in the manner of Java.util 数学. The case of override being not required is pretty intuitive; almost always you would know.
public class Test {
public static void main(String args[]) {
ArrayList<Student> a = new ArrayList<Student>();
a.add(new Student("Steve", 12, "Daniel"));
a.add(new Student("Sachin", 10, "Tendulkar"));
System.out.println(a);
display(a);
}
static void display(ArrayList<Student> stu) {
stu.add(new Student("Yuvi", 12, "Bhajji"));
System.out.println(stu);
}
}
Student.java:
public class Student {
public String name;
public int id;
public String email;
Student() {
}
Student(String name, int id, String email) {
this.name = name;
this.id = id;
this.email = email;
}
public String toString(){ //using these toString to avoid the output like this [com.steve.test.Student@6e1408, com.steve.test.Student@e53108]
return name+" "+id+" "+email;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public int getId(){
return id;
}
public void setId(int id){
this.id=id;
}
public String getEmail(){
return email;
}
public void setEmail(String email){
this.email=email;
}
}
If you are not used toString() in Pojo(Student.java) class,you will get an output like [com.steve.test.Student@6e1408, com.steve.test.Student@e53108].To avoid these kind of issue we are using toString() method.