Why use Int where Byte or Short fits?

Asked

Viewed 864 times

2

Why not use the primitive type byte or short to store an age of person instead of using the type int? The two mentioned consume less memory? Wouldn’t it be better?

  • 1

    This is something that is at the discretion of the developer, in analyzing which types of data are best suited to the context of the application he is developing. There is no convention that says you have to use int type when you want to represent a person’s age in an attribute.

  • It is because in many of the examples the galley only uses the int, and almost do not see examples with the type byte the short ..

  • 3

    Related? Why wear shorts?

2 answers

4

The simple answer is: why you don’t need to make this economy.

Are you sure it consumes less memory? If it is used in a data structure, which is quite common in real code, there will be alignment and this lower consumption can disappear.

Let’s say you have the gain, help something real? Even more so using Java that is memory-consuming for other reasons. It’s trying to contain a flood.

On some architectures it is faster to process an integer than a type of another size. Want to have this loss? Imagine how much is spent having to make conversions in several cases to make the code generic or to make it compatible with what is ready.

Of course if you need the gain, you have to measure it, I can tell you that for most cases it is not worth the effort or there is no gain in doing this. This is why people prefer to use the most general and standard way. Using a smaller type is an optimization and for every optimization there must be a real good justification, no speculation.

For more details have a reply from me at another question. It’s C#, but it goes to Java.

  • You understand but my doubt was in relation to the basic same, that many people do not make use of these two types . You will hardly see anyone ultilizing even in basic examples, as create a variable that stores a person’s age byte and short type .

  • I don’t think you understand. I explained that. But if you want to know more explicitly why it is not used in basic examples, it is because since they do not do anything useful, why try to optimize? People don’t try to optimize something just because they’re pragmatic, they know what they’re doing and they know that you don’t have to do it, that there’s no real gain or if there is, it’s useless. Of course not all of them. Some do it right because they’ve copied the ones they know how to do.Copying what others do may or may not work is lucky.That’s why it’s important to know what you’re doing to make the right decision

3


Nothing stops you from wearing this.

What happens in the market (at least I’ve seen this in three places) is that people standardize.

In one of these places was VB6 with oracle, they had a whole layer of persistence of them to do everything they needed bank access, it was a large system, over 5000 terminals. In their case the use of some different types gave error in that layer of persistence of them, and they didn’t want to change anything.

If it’s good or bad I won’t get into the merits.

What I think is that today with all the tools that exist it is worth thinking about how to store the information in a cleaner, correct, beautiful way.

  • Well didactic your answer, I know that to persist with bank Ultilizamos the Wrappers as Integer, Double etcc..., but like we do not always need to ultiliza the same to guard an age, I’ve been studying java and I notice that in all tutorials the guys only use int to store variables that receive decimal numbers between 0 and 100 and I do not understand why , understand ?

  • Speaking of tutorial you’re talking about rice bean, it’s easier for everyone to understand, but when it comes to large systems you have frameworks running from both third party and private and this often dictates what will be used, Your idea of using the right guys is a great idea and you should keep thinking that. Just don’t be a fanatic in case you ever see anything different :)

  • rsrsr Thanks for the feedback ..

Browser other questions tagged

You are not signed in. Login or sign up in order to post.