Which data type is appropriate to store the age of the person?
I will clearly say that to store the age of the person, the appropriate data type is an int data type. However, if you are trying to reduce space complexity, I will say that you should go for byte data type as it has a range of -128 to 127. On the other hand, int data type ranges from -2,147,483,648 to 2,147,483,647. So byte data type would be perfect as the age of a person is more likely to be smaller than 127 (the maximum number which a byte data type can store).
I would generally prefer int data type because of the following reasons:
- We should not use float data type to store age as it would be inappropriate because the age of a person is generally preferred in numbers and not the decimal format. For Example, We say that the Age of a person is 18 rather than saying that that the age of the person is 18.0 or something like that.
- We should not use the String data type as it would be tricky to handle data.
- We can not use character data type as it only stores 1 bit ie only a single character. So, we can not store age in limits 0 to 9.
- We should store age in integer data type as the int data type is easy to manipulate and to perform mathematical operations.
- Last but not the least, I would suggest int data type to store age as it would be more general data type as compared to the byte data type. However, it is a good practice to reduce space complexity and we should do it to make our program more efficient and fast in some cases.
In order to store a large amount of data, for example, to store the age of the 1000 person then we should go for byte data type as it would save a large number of bits from being wasted as it remains unused throughout the program.
So, at last, we can conclude that the data type which is appropriate to store the age of the person is the int data type. In case when you need to save some space then definitely go for the byte data type.
I hope that this answer will help you to make your final decision to choose any one data type between int and byte for storing age. If you have any question then do not keep it in your mind, put in front of all so that everyone could get its answer.
If you have any questions, feel free to comment below. Subscribe to this blog to get the answers to the latest programming question. You should check my other programming blogs too.
Programming Chaska - where you can get several programs in C, C++, and JAVA
Programming Chaska - where you can get several programs in C, C++, and JAVA
Comments
Post a Comment