有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (4) 个答案

  1. # 1 楼答案

    看看这个:

    15.18.1. String Concatenation Operator +

    If only one operand expression is of type String, then string conversion (§5.1.11) is performed on the other operand to produce a string at run time.

    JLS 15.18.1

  2. # 2 楼答案

    你必须在这里给出优先顺序。这样一来,具有求和的括号将首先执行,然后执行串联。否则它是从左到右评估的。最左边的文字是String,您将数字连接到String文字,从而得到上面的结果"2 + 2 = 22"

    System.out.println("2 + 2 = " + (2 + 2));
    
  3. # 4 楼答案

    如果+的任何一个参数是字符串,那么返回的也是字符串

    考虑到这一点,你可以得到:

    "2 + 2 = " + 2=>"2 + 2 = 2"

    "2 + 2 = 2" + 2=>"2 + 2 = 22"

    如果你想在字符串连接之前进行数学运算,你需要确保它首先发生,正如@Ravindra的答案所示