Difference between x.toString() and x+“”

后端 未结 8 2007
暗喜
暗喜 2020-12-11 22:50

Back in college one of my profs. taught us to just do x + \"\" as a quick conversion from basic types to strings.
I don\'t remember which class it was in I

8条回答
  •  醉话见心
    2020-12-11 23:41

    In Java, for primitive types (int, double, etc.) you cannot write .toString() because the types aren't objects. This means that your options are either to write something like

    x + "";
    

    or to use

    Integer.toString(x);
    

    In C++, you cannot use x + "" to do this sort of conversion, since this will be treated as pointer arithmetic and will give you a bad pointer. Using something like boost::lexical_cast is the preferred way to do the conversion.

    And... I know nothing about C#, so I won't comment on it. :-)

提交回复
热议问题