如何从整数到char的隐式类型转换在Java中工作(Java基础知识)

use*_*267 0 java android

所以在这个基本的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中是如何工作的呢?

roc*_*boy 5

它不是一种隐含的转换.该pintln()重载通过大量的数据类型:

public void println(int x) {
    synchronized (this) {
        print(x);
        newLine();
    }
}
Run Code Online (Sandbox Code Playgroud)

在你的情况下makeText()不是.