Why not use the primitive type byte
or short
to store a person's age instead of using type int
? Do the two mentioned consume less memory? Would not that be better?
Why not use the primitive type byte
or short
to store a person's age instead of using type int
? Do the two mentioned consume less memory? Would not that be better?
Nothing prevents you from using this.
What happens in the market (at least I've seen this in three places) is that people standardize.
In one of those places was VB6 with oracle, they had a whole layer of persistence from them to do everything they needed to access the bank, it was a large system, more than 5000 terminals. In their case the use of some different types gave error in that layer of their persistence, and they did not want to change anything.
Whether this is good or bad I will not go into merit.
What I think is that today with all the tools out there it's worth thinking about how to store the information in a clean, correct, beautiful way.
The simple answer is: because you do not need to make this savings.
Are you sure you consume less memory? If it is used in a data structure, which is very common in real code, there will be alignment and this smaller consumption may disappear.
Let's say you have the win. Help something real? Even more so using Java that is memory hog for other reasons. It's trying to contain drips from a flood.
In some architectures it is faster to process an integer than a type of another size. Do you want to have this loss? Imagine how much you spend having to do conversions in a number of cases to make the code generic or to match what's ready.
Of course you need the gain, you have to measure, but I can tell you that in most cases it is not worth the effort or there is no gain in doing this. This is why people prefer to use the more general and standard way. Using a smaller type is an optimization and for any optimization there must be a real good justification, no speculation.
For more details, see my answer in another question . It's C #, but good for Java.