所以在这个基本的Java程序中,
public class HelloWorld{
public void testFunc(){
System.out.println("Class = "+this);
}
@Override
public String toString(){
return "TEST";
}
public static void main(String[] args){
int i = 5;
HelloWorld hw = new HelloWorld();
System.out.println("Hello, World");
hw.testFunc();
System.out.println(i);
}
}
Run Code Online (Sandbox Code Playgroud)
该行System.out.println(i);自动将其转换int i为a String.我认为任何需要String类型参数的函数都会在java中自动执行此转换(与C中不同).但是,如果我在Android
Toast.makeText(getApplicationContext(), i, Toast.LENGTH_SHORT).show();
中尝试将其i声明为int扩展活动类中的一个,则应用程序会在启动时崩溃.那么这种转换在Java中是如何工作的呢?
它不是一种隐含的转换.该pintln()被重载通过大量的数据类型:
public void println(int x) {
synchronized (this) {
print(x);
newLine();
}
}
Run Code Online (Sandbox Code Playgroud)
在你的情况下makeText()不是.
| 归档时间: |
|
| 查看次数: |
543 次 |
| 最近记录: |