Week 137 — What primitive types does Java provide and how do they differ?
Question of the Week #137
What primitive types does Java provide and how do they differ?
5 Replies
Java comes with eight primitive types that can be grouped into three categories:
-
boolean
can be either true
or false
.
- The integral types byte
, char
, short
, int
and long
can be used to store whole numbers but not floating point numbers. The byte
type represents an 8-bit value, char
and short
are 16-bit values, int
s are used for 32-bit values and long
can be used for 64-bit values. The char
type is unsigned (cannot contain negative numbers) and typically used to represent UTF-16 code units (for representing text) while the other integral types are signed integers that allow both positive and negative values.
- The floating point types float
(32-bit) and double
(64-bit) store IEEE 754 floating point numbers. IEEE 754 is a specification for floating point numbers that are stored using a sign, a mantissa and an exponent. This allows storing values as close to 0 as 4.9 * 10^-324
up to 1.7976931348623157 * 10^308
for double
. However, this range of numbers comes at the cost of precision as it doesn't store the full number but only the most significant binary digits. IEEE 754 reserves special values for positive and negative 0, positive and negative infinity as well as NaN (not a number).📖 Sample answer from dan1st
Java provides 8 primitive types:
-
boolean
- byte
- short
- int
- long
- float
- double
- char
boolean
can only represent the values true
and false
. It is the only type that can be used on its own directly in conditional contexts like if
statements and ternary expressions, because those expect a boolean value.
The types byte
, short
, int
, and long
represent signed integral values. They differ from each other in the number of bits that are used to represent them, and hence the range of values they can contain.
- A byte
is 8 bits, and holds values from -128 to 127
- A short
is 16 bits, and holds values from -32,768 to 32,767
- An int
is 32 bits, and holds values from -2,147,483,648 (-2^31) to 2,147,483,647 (2^31-1)
- A long
is 64 bits, and holds values from -9,223,372,036,854,775,808 (-2^63) to 9,223,372,036,854,775,807 (2^31-1)
The floating point types are float
(32 bits) and double
(64 bits). Floating point numbers follow the IEEE 754 standard, which makes them somewhat imprecise. Not all values can be exactly represented using float
& double
.
Java provide 8 different primitive data types, with each representing simple value with no additional methods or behaviours.
The Eight Primitive Types:
char
is a 16-bit representation of a single Unicode (UTF-16) character from \u0000
to \uFFFF
. Java Strings are made up of sequences of char
values.Submission from dangerously_casual
Details:
-
byte
, short
, int
, and long
are all integer types. They differ only by the number of bits and their value range.
- float
and double
are floating-point types, with double
having higher precision (more decimal points, larger range).
- char
stores a single 16-bit Unicode character. Other many languages only use 8 bits for char (ASCII), due to the difference it allows Java to represent far more symbols.
- All primitive values are immutable; assignment copies the value, not a reference.
- Primitive types in Java aren’t objects, they don’t have fields or methods.- Java promotes types in expressions. If you assign a larger type to a smaller, a cast is required:
Submission from daysling