问题
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