简体   繁体   中英

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

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) ;

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);

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM