Why is there no String.valueOf(short a);

本小妞迷上赌 提交于 2019-12-11 12:32:27

问题


I had to cast short to a string and the only way I could do it was to concatenate an empty string with it, is this the right way?

PS, why is there no String.valueOf(short a);


回答1:


Because the creators of the JDK didn't think that it is needed.

Given a short you can simply call String.valueOf(int).

Note that even Short.toString(short) is implemented as return Integer.toString((int)s, 10);




回答2:


You can use

 short s = 5
 String str = String.valueOf(s); // s is widened to int.

or

 String str = Short.toString(s);

adding a String.valueOf(short) wouldn't change much.




回答3:


String.valueOf(short a); is not needed because String.valueOf(int a); is equivalent so will be redundant in String API.

In order to use Short built in function use:

Short.toString(short);

Also, as explained and as Jon Skeet pointed:

Why not just use the fact that it's implicitly convertible to int? short x = 10; String y = String.valueOf(x); should work fine

String.valueOf(int);



来源:https://stackoverflow.com/questions/32945817/why-is-there-no-string-valueofshort-a

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!