understanding the java string with add operator

前端 未结 4 1939
轮回少年
轮回少年 2021-01-16 04:04

I am trying to understand how the compiler views the following print statements. It is simple yet a bit intriguing.

This prints the added value. Convincing enough.<

4条回答
  •  长情又很酷
    2021-01-16 04:28

    I guess the Java specification explains it best:

    The + operator is syntactically left-associative, no matter whether it is determined by type analysis to represent string concatenation or numeric addition. In some cases care is required to get the desired result. For example, the expression:

    a + b + c

    is always regarded as meaning:

    (a + b) + c

    So, if a was a String, then it concatenates with b. The result would be of String type, hence it continues to concatenate with c.

提交回复
热议问题