比如我的测试程序: public class Test{ void p(){ System.out.println(this); } public static void main(String[] args){ Test a=new Test(); a.p(); } } 输出结果: test.Test@107077e test只的是你的包路径. Test指的是你的类 @107077e指的是实例对象 a
把一个对象的引用传递给System.out.println()方法, 方法会自动调用该对象的toString()方法, 并打印出toString()返回的字符串.因为所有对象都继承自Object, 所以你去了解一下Object类的toString()方法的具体实现就知道是怎么回事了. (如楼上几位所说)如果你在你自己的类里重写了toString()方法, 那么println()打印出的将是你的新的toString()方法返回的串. 如:public class Test { public void printMe() { System.out.println(this); } public String toString() { return "Hello, world!"; } public static void main(String[] args) { new Test().printMe(); //will print "Hello, world!" }}
public class Test{
void p(){
System.out.println(this);
}
public static void main(String[] args){
Test a=new Test();
a.p();
}
}
输出结果: test.Test@107077e
test只的是你的包路径.
Test指的是你的类
@107077e指的是实例对象 a
System.out.println(this);
} public String toString() {
return "Hello, world!";
} public static void main(String[] args) {
new Test().printMe(); //will print "Hello, world!"
}}