小编Ret*_*ang的帖子

在Java中,为什么输出int a =('a'+'b'+'c'); 是不同的形式System.out.println('a'+'b'+'c'+"")

原来的问题是这样的.

public class test {
    public static void main(String[] args){
        int i = '1' + '2' + '3' + "";
        System.out.println(i);
    }
}
Run Code Online (Sandbox Code Playgroud)

这给了我一个错误:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Type mismatch: cannot convert from String to int
Run Code Online (Sandbox Code Playgroud)

然后我改变了这样的代码:

public class test {
    public static void main(String[] args){
        int i = '1' + '2' + '3';
        System.out.println(i);
    }
}
Run Code Online (Sandbox Code Playgroud)

输出是150.

但是当我写这样的代码时:

public class test {
    public static void main(String[] args){
        System.out.println('a'+'b'+'c'+"");
    }
}
Run Code Online (Sandbox Code Playgroud)

输出变为294.

我想知道为什么.

java

7
推荐指数
1
解决办法
5010
查看次数

标签 统计

java ×1