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
Eric McIntyre
Eric McIntyre3mo ago
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, ints 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
Eric McIntyre
Eric McIntyre3mo ago
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:
| Type | Size (bits) | Default | Example |
|:-------:|:-----------:|:--------:|:---------------------:|
| byte | 8 | 0 | byte b = 100; |
| short | 16 | 0 | short s = 20000; |
| int | 32 | 0 | int i = 123456; |
| long | 64 | 0L | long l = 9000000000L; |
| float | 32 | 0.0f | float f = 3.14f; |
| double | 64 | 0.0d | double d = 2.71828; |
| char | 16 | '\u0000' | char ch = 'A'; |
| boolean | 1 | false | boolean b = true; |
| Type | Size (bits) | Default | Example |
|:-------:|:-----------:|:--------:|:---------------------:|
| byte | 8 | 0 | byte b = 100; |
| short | 16 | 0 | short s = 20000; |
| int | 32 | 0 | int i = 123456; |
| long | 64 | 0L | long l = 9000000000L; |
| float | 32 | 0.0f | float f = 3.14f; |
| double | 64 | 0.0d | double d = 2.71828; |
| char | 16 | '\u0000' | char ch = 'A'; |
| boolean | 1 | false | boolean b = true; |
Eric McIntyre
Eric McIntyre3mo ago
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
Eric McIntyre
Eric McIntyre3mo ago
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.
Eric McIntyre
Eric McIntyre3mo ago
- Java promotes types in expressions. If you assign a larger type to a smaller, a cast is required:
int small = (int) 1_000_000_0000L; // possible narrowing conversion
int small = (int) 1_000_000_0000L; // possible narrowing conversion
Submission from daysling

Did you find this page helpful?