user3779397
user3779397

Reputation: 11

Format specifier for data type "short" in JAVA

please tell what format specifier is used for "short" data type in java? Like %d is used for integer %s is used for string.

Upvotes: 0

Views: 1311

Answers (3)

Pphoenix
Pphoenix

Reputation: 1473

From primitive data types:

short: The short data type is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). As with byte, the same guidelines apply: you can use a short to save memory in large arrays, in situations where the memory savings actually matters.

A short is an integer, so using %d is the correct way.

Upvotes: 1

PurkkaKoodari
PurkkaKoodari

Reputation: 6809

There is none. short is just a 16-bit integer, so use %d.

If you want an unsigned short, use this code.

short s;
int unsignedShort = ((int) s) & 0xFFFF;

and use that value with %d.

Upvotes: 1

Troncoso
Troncoso

Reputation: 2463

You can use %d as if it were an integer.

Upvotes: 0

Related Questions