相关疑难解决方法(0)

在字符串文字之后,所有的+都会被视为字符串连接运算符么?

我还没有理解为什么在连接中将整数视为字符串文字.例如

String s=10+30+" Sachin "+40+40;  
System.out.println(s);
Run Code Online (Sandbox Code Playgroud)

输出是:40 Sachin 4040.

为什么40+40没有添加,为什么10+30要添加?

java

5
推荐指数
1
解决办法
329
查看次数

一起使用 String 和 int 时的 System.out.println 行为

考虑下面的代码片段 -

public class Student {

    public static void main(String args[]) {
        int a = 3;
        int b = 4;
        System.out.println(a + b +" ");
        System.out.println(a + b +" "+ a+b);
        System.out.println(""+ a+b);

    }
}
Run Code Online (Sandbox Code Playgroud)

上述代码片段的输出如下:

7
7 34
34
Run Code Online (Sandbox Code Playgroud)

从输出中可以清楚地看出,如果我们首先在 print 语句中使用 String,那么整数将被连接起来。但是,如果我们首先使用整数,那么这些值将被添加并显示。

有人可以解释一下为什么会出现这种行为吗?

我什至尝试查看 PrintStream 类中 println() 方法的实现,但无法弄清楚。

java string int operation

3
推荐指数
1
解决办法
1万
查看次数

标签 统计

java ×2

int ×1

operation ×1

string ×1