Concatenation of Strings and characters

后端 未结 5 1776
日久生厌
日久生厌 2020-12-24 10:46

The following statements,

String string = \"string\";   

string = string +((char)65) + 5;
System.out.println(string);

Produce the output <

5条回答
  •  心在旅途
    2020-12-24 11:01

    It is the same case as the example:

    System.out.println("abc"+10 + 5);
    

    produces abc105 (add as String)

    and

    System.out.println(5 + 10);
    

    produces 15 (add as Number)

    The String included in the (first place of the) operation forces all the elements to be treated as Strings while executing the operation. In your case with += however, the operation is executed first in the right part (treating the elements as int) because of the operator precedence and then is concatenated with the String.

提交回复
热议问题