About Lesson
Scenario: You are working on an application that tracks user ages. The ages range from 0 to 150. What would you choose: int
, short
, or byte
? Explain your choice.
Answer:
The most appropriate choice here is short
. The reason is that int
is larger than necessary for storing ages in this range (0 to 150), and byte
only supports values from -128 to 127, so it won’t work for ages up to 150. A short
can hold values between -32,768 and 32,767, making it both memory-efficient and capable of handling the required range.
short age = 25; // Appropriate data type for ages in the range of 0-150