当前位置: 技术问答>java相关
菜鸟问题系列2 关于string
来源: 互联网 发布时间:2015-07-30
本文导语: 我又碰到一个菜问题,先看下面的程序: MyPoint.java public class MyPoint { public int x; public int y; public String toString() { return ("[" + x + "," + y + "]"); } } ...
我又碰到一个菜问题,先看下面的程序:
MyPoint.java
public class MyPoint {
public int x;
public int y;
public String toString() {
return ("[" + x + "," + y + "]");
}
}
TestMyPoint.java
public class TestMyPoint {
public static void main(String[] args) {
// Step 1: declare variables
MyPoint start = new MyPoint();
MyPoint end = new MyPoint();
// Step 2: assign data attributes of start and end
start.x = 10;
start.y = 10;
end.x = 20;
end.y = 30;
// Step 3: print out start and end
System.out.println("Start point is " + start);
System.out.println("End point is " + end);
System.out.println();
// Step 5: declare and assign stray to end
MyPoint stray = end;
// Step 6: declare and assign stray to end
System.out.println("Stray point is " + stray);
System.out.println("End point is " + end);
System.out.println();
// Step 7: assign new values to stray
stray.x = 47;
stray.y = 50;
System.out.println("Stray point is " + stray);
System.out.println("End point is " + end);
System.out.println("Start point is " + start);
}
}
输出结果:
Start point is [10,10]
End point is [20,30]
Stray point is [20,30]
End point is [20,30]
Stray point is [47,50]
End point is [47,50]
Start point is [10,10]
这是一个练习Reference Variable Assignment的例子,让我疑惑的是为什么能够直接显示Start、End、Stray(它们是reference variable),而不用调用Method toString?但是看显示的结果,似乎已经调用过toString。难道是编译器自动执行的?Java里有这样的规定吗?我读到目前的章节并没有介绍过相关的内容,以前学C++也没有类似的情况,这种突然出现又无法解释的问题真让人费解~~~~~~
为此我又翻阅了侯捷译的《Thinking In Java 2e》,里面举的是Primitive Variable的例子:
int x=0;
String sString="…………";
System.out.println(sString+x);
编译器会自动将int 0转换成string "0"
照这样看,可以认为编译器也会自动转换reference variable,正如上程序自动调用了Method toString。但是我又想到的是,如果没有Method toString,结果又会怎么样?我自己改了代码,运行结果如下:
Start point is MyPoint@256a7c
End point is MyPoint@720eeb
Stray point is MyPoint@720eeb
End point is MyPoint@720eeb
Stray point is MyPoint@720eeb
End point is MyPoint@720eeb
Start point is MyPoint@256a7c
结果是我看不懂的东西,它是allocate给MyPoint的instance的space的地址吗?
照这样看,还是缺不了Method toString。在要显示为string的情况下,必须要先declare Method toString吗?java是这样规定的吗?
可不可以不叫toString,改个名字,还是硬性规定为toString?
我实在是很疑惑,有没有人能指导一下我~~~~~~~~~~
MyPoint.java
public class MyPoint {
public int x;
public int y;
public String toString() {
return ("[" + x + "," + y + "]");
}
}
TestMyPoint.java
public class TestMyPoint {
public static void main(String[] args) {
// Step 1: declare variables
MyPoint start = new MyPoint();
MyPoint end = new MyPoint();
// Step 2: assign data attributes of start and end
start.x = 10;
start.y = 10;
end.x = 20;
end.y = 30;
// Step 3: print out start and end
System.out.println("Start point is " + start);
System.out.println("End point is " + end);
System.out.println();
// Step 5: declare and assign stray to end
MyPoint stray = end;
// Step 6: declare and assign stray to end
System.out.println("Stray point is " + stray);
System.out.println("End point is " + end);
System.out.println();
// Step 7: assign new values to stray
stray.x = 47;
stray.y = 50;
System.out.println("Stray point is " + stray);
System.out.println("End point is " + end);
System.out.println("Start point is " + start);
}
}
输出结果:
Start point is [10,10]
End point is [20,30]
Stray point is [20,30]
End point is [20,30]
Stray point is [47,50]
End point is [47,50]
Start point is [10,10]
这是一个练习Reference Variable Assignment的例子,让我疑惑的是为什么能够直接显示Start、End、Stray(它们是reference variable),而不用调用Method toString?但是看显示的结果,似乎已经调用过toString。难道是编译器自动执行的?Java里有这样的规定吗?我读到目前的章节并没有介绍过相关的内容,以前学C++也没有类似的情况,这种突然出现又无法解释的问题真让人费解~~~~~~
为此我又翻阅了侯捷译的《Thinking In Java 2e》,里面举的是Primitive Variable的例子:
int x=0;
String sString="…………";
System.out.println(sString+x);
编译器会自动将int 0转换成string "0"
照这样看,可以认为编译器也会自动转换reference variable,正如上程序自动调用了Method toString。但是我又想到的是,如果没有Method toString,结果又会怎么样?我自己改了代码,运行结果如下:
Start point is MyPoint@256a7c
End point is MyPoint@720eeb
Stray point is MyPoint@720eeb
End point is MyPoint@720eeb
Stray point is MyPoint@720eeb
End point is MyPoint@720eeb
Start point is MyPoint@256a7c
结果是我看不懂的东西,它是allocate给MyPoint的instance的space的地址吗?
照这样看,还是缺不了Method toString。在要显示为string的情况下,必须要先declare Method toString吗?java是这样规定的吗?
可不可以不叫toString,改个名字,还是硬性规定为toString?
我实在是很疑惑,有没有人能指导一下我~~~~~~~~~~
|
toString()是Object提供的方法之一,当编译器遇到system.out.println(obj)时,会自动调用obj的toString()方法,如果在自己的类中没有覆盖它,会调用Object中的该方法,得到对象的地址。
至于Object还有什么方法,请参考Java Api 文档
至于Object还有什么方法,请参考Java Api 文档
|
toString()是基类Object的方法,在这个方法里面toString()返回字符串的格式为:类名◎地址.你自己不覆盖基类的方法,它就缺省调用基类的方法,你的MyPoint的基类是Object,所以就出现上面的结果....
|
Effective Java这本书里有一章就叫"永远要超载toString()"
|
在System.out.println()处理类类型时 会自动调用该对象的toString()方法