I am relatively new so there is a good chance I'm not grasping something. However, I don't even know if I am asking this question properly... hahaha... but, here she goes!
Since the range of an int covers the range of char, byte, and short, in that, when a term exceeds the range of char, byte, and short, java automatically converts the operand to int, what is the need for byte, short, char operands (in terms of expressions or intermediate terms)?
The integer types apart from int and char are only important if you:
- deal with binary formats or low level web requests
- write embedded applications, e.g. SmarCard programming, where you are short on space
- you have large arrays containing integers, where a more narrow type can indeed save a lot of space
The char type is another matter, as it has different semantics. Also note that it is unsigned, which the other types aren't. As sherlok noted, marking a variable as char will tell the reader of the code that it is used for characters and (hopefully) nothing else. You should always use types to clarify your semantics to prevent bugs.
Example: I wrote a game where I had to deal with two kinds of location descriptions on the screen. One was the location in pixels, the other the location in game tiles. I did the mistake to use just int x and int y for both of them with the result of having to fix mostly issues that where related to not knowing or forgetting which kind of location i was dealing with right now. Solution: Two data classes, PixelLocation and TileLocation, used like structs to hold the values. I didn't pass (int x, int y) anymore, but PixelLocation or TileLocation, depending which one was used. This really clarified things and it made it impossible to use the wrong type.
Firstly using a smaller datatype requires less space (ram) at runtime.
However in practice people tend to prefer int to short, simply because they are used to using int and for normal applications it won't make much of a difference.
Unless you have arrays, the JVM won't make a difference handling short, byte or int. Local variables of these types all occupy 32 bit cells within the JVM despite their actual length.
The integer operations are optimized for int values. Using byte or short instead of int is most of the time less efficient as the operations are slower.
Memorywise it is really only a good choice if dealing with very large arrays.
Unless you have performance or memory issues, you shouldn't optimize anyway as it does more harm than good.