且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

隐式转换为字符串toString和int +“”

更新时间:2022-11-09 07:56:31

不,它的工作原理是 String.valueOf(a)+,它的行为类似 new StringBuilder(String.valueOf(a)).append().toString()

No, it works like String.valueOf( a ) + "", which in turn behaves like new StringBuilder( String.valueOf( a ) ).append( "" ).toString().

重要的是要知道它是由编译器完成的,换句话说它是语法糖。这就是为什么在循环中添加字符串不是一个好主意。 (虽然现代虚拟机可能有一些机制来降低性能开销。)

The important thing to know is that it's all just done by the compiler, in other words it's syntactic sugar. This is why adding strings together in a loop isn't a good idea for example. (Although modern VMs might have some mechanism to reduce the performance overhead.)