primitive-boolean To String concatenation/conversion

前端 未结 3 1558
别那么骄傲
别那么骄傲 2021-01-17 22:56

how does this work? I can\'t seem to find an answer.

boolean bool=true;
System.out.println(\"the value of bool is : \" + true);
//or
System.out.println(\"the         


        
3条回答
  •  迷失自我
    2021-01-17 23:03

    It's a compiler thing. If the right operand for concatenation is an object, the object is sent the toString() method whereas if the operand is a primitive then the compiler knows which type-specific behavior to use to convert the primitive to an String.

提交回复
热议问题