Junior — Middle
How do ranges of possible sizes work and what values can they take for simple data types?
sobes.tech AI
Answer from AI
In Java, primitive data types have fixed sizes and value ranges:
- byte: 8 bits, values from -128 to 127
- short: 16 bits, values from -32,768 to 32,767
- int: 32 bits, values from -2,147,483,648 to 2,147,483,647
- long: 64 bits, values from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
- float: 32-bit floating point number, approximate range ±1.4E-45 to ±3.4E+38
- double: 64-bit floating point number, approximate range ±4.9E-324 to ±1.7E+308
- char: 16 bits, represents a Unicode character, values from 0 to 65,535
- boolean: takes values true or false (size not strictly defined, depends on JVM)
These sizes and ranges are defined by the Java specification and allow efficient use of memory and computations.