Why does the Java API use int instead of short or byte?

Why does the Java API use int if short or even byte would be sufficient?

Example: The DAY _ OF _ WEEK field in Calendar uses int.

1 Like

In Java, byte and short are meant for special purposes. Although, if you are operating on data which fits well in short or byte, you can use short or byte instead of int. But they serve some other useful purposes too. For example, byte can be used while handling a binary stream. But we don’t want to add or subtract each binary bit we receive from the stream. And therefore, addition rules are slightly different for int and byte. Example:
byte a=2, b=1;
byte c=a+b; //error
byte c=(byte)(a+b); // works fine
Same rules are for short data type.

1 Like