Type conversion of int and string, java

前端 未结 5 619
借酒劲吻你
借酒劲吻你 2021-01-02 00:23

last exam we had the exercise to determine the output of the following code:

System.out.println(2 + 3 + \">=\" + 1 + 1);

My answer was <

5条回答
  •  青春惊慌失措
    2021-01-02 00:33

    Let's read it one token at a time from left to right:

    The first literal encountered is an integer, 2, then a +, then another integer, 3. A + between two integers is addition, so they are added together to be 5.

    Now we have 5, an integer, then a +, then a String ">=". A + between an integer and a String is a concatenation operator. So the Strings are combined to form "5>=".

    Then we have "5>=", a String, a +, and then an integer, 1. This is String concatenation again. So the result is "5>=1".

    Finally we have "5>=1", a String, a +, and the a 1. his is String concatenation again. So the result is "5>=11".

提交回复
热议问题