Primitive Types
Java has eight primitive data types that form the basic building blocks for storing simple values. Unlike reference types, primitives represent values directly rather than references to objects. They are divided into four groups: integer types, floating-point types, character, and boolean.
Why Java Has Primitive Types
Java supports objects and reference types for complex data, but using objects for every simple value would introduce unnecessary overhead. Primitive types provide a simpler and more efficient way to represent numbers, characters, and logical values. They have predefined sizes and behavior, making them useful for calculations, counters, flags, indexes, and many other everyday programming tasks.
Java Primitive Types
The eight primitive types are byte, short, int, long, float, double, char, and boolean. The first four represent whole numbers, float and double represent decimal values, char represents a single character, and boolean represents a logical value.
Integer Types
Java provides four integer types: byte, short, int, and long. They store whole numbers and differ mainly in their storage size and range. byte uses 1 byte, short uses 2 bytes, int uses 4 bytes, and long uses 8 bytes.
In normal Java development, int is the most commonly used integer type. It is suitable for quantities, counters, indexes, and most ordinary whole-number calculations. long is used when a value can exceed the range of int, such as large counters, timestamps, or calculations involving very large values. byte and short are less common and are mainly useful when their smaller ranges or memory characteristics are specifically relevant.
| Type | Size | Range | Common Use |
|---|---|---|---|
| byte | 1 byte | -128 to 127 | Small numeric values |
| short | 2 bytes | -32,768 to 32,767 | Less common numeric values |
| int | 4 bytes | -2,147,483,648 to 2,147,483,647 | Counters, quantities, indexes |
| long | 8 bytes | About -9.22 × 10¹⁸ to 9.22 × 10¹⁸ | Large values, timestamps |
int quantity = 25;long views = 9500000000L;
byte rating = 5;
short year = 2026;Integer literals are normally treated as int. When a value is too large for int and needs to be represented as a long literal, an uppercase L is added to the end.
Java also supports decimal, hexadecimal, and binary integer literals.
int decimal = 26;int hexadecimal = 0x1A;
int binary = 0b11010;These three values represent the same number using different number systems.
Floating-Point Types
The floating-point types are float and double. They are used for numbers containing fractional values. float uses 4 bytes and provides less precision, while double uses 8 bytes and provides greater precision. double is the usual choice for general-purpose floating-point calculations.
float rating = 4.5f;double price = 29.99;A decimal literal is treated as double by default. Therefore, when assigning a decimal literal to a float variable, the f suffix is required.
Floating-point values should be used carefully when exact decimal calculations are required. Binary floating-point numbers cannot represent every decimal fraction exactly, which can produce small rounding differences.
double result = 0.1 + 0.2;System.out.println(result);The result can be 0.30000000000000004 rather than exactly 0.3. For financial applications where exact decimal calculations are required, a decimal type such as BigDecimal is more appropriate.
The char Type
The char type represents a single 16-bit UTF-16 code unit. Character literals are written using single quotation marks.
char grade = 'A';char symbol = 'A character and a String are different types. A single character uses single quotation marks, while text uses double quotation marks.
char letter = 'A';String name = "Java";Because char has a numeric representation, arithmetic can also be performed on it.
char letter = 'A';char next = (char) (letter + 1); System.out.println(next);This produces B.
Escape Sequences
Java provides escape sequences for characters that are difficult or impossible to write directly.
Escape Meaning \n New line \t Tab \r Carriage return \ Backslash ' Single quotation mark " Double quotation mark For example:
System.out.println("Java\nProgramming");The output appears on two lines.
The boolean Type
The boolean type represents a logical condition and has only two possible values: true and false. It is commonly used for conditions and flags such as whether a product is available or whether an order has been completed.
boolean inStock = true;boolean orderCompleted = false;Java does not automatically treat numbers or strings as boolean values. For example, 0 is not automatically considered false, and an empty String is not automatically considered false.
Default Values
Primitive fields receive default values when they are declared without an explicit initial value. Numeric types receive zero, boolean receives false, and char receives the null character.
Type Default Value byte 0 short 0 int 0 long 0L float 0.0f double 0.0 char null character boolean false Local variables behave differently. Java does not automatically initialize local variables with default values. A local variable must be assigned a value before it is read.
int quantity;quantity = 10; System.out.println(quantity);Trying to read quantity before assigning a value causes a compilation error.
Integer Overflow
Integer types have fixed ranges. When an integer calculation exceeds the maximum value that the type can represent, the value can wrap around to the opposite end of the range.
int max = Integer.MAX_VALUE;int result = max + 1; System.out.println(result);The result becomes the minimum int value instead of producing a larger number. This is important when performing calculations involving large quantities.
When a calculation can exceed the range of int, use long and make sure the calculation itself is performed using long.
long total = (long) quantity * price;The conversion happens before multiplication, so the calculation uses the larger numeric range.
Choosing the Right Primitive Type
For most whole-number values, int is the natural choice. Use long when the required value can exceed the range of int. byte and short are useful in specific memory-sensitive situations, especially when working with large arrays.
For general decimal calculations, double is normally preferred over float. For exact monetary calculations, avoid floating-point types and use an appropriate decimal representation.
Use char when working with individual characters and boolean when representing a true or false condition.
Summary
Java provides eight primitive types: byte, short, int, long, float, double, char, and boolean. Integer types handle whole numbers, floating-point types handle fractional values, char represents a UTF-16 code unit, and boolean represents logical values. Understanding primitive types is essential because they are used throughout Java for variables, calculations, conditions, method parameters, arrays, and many other programming tasks.
;
A character and a String are different types. A single character uses single quotation marks, while text uses double quotation marks.
char letter = 'A';String name = "Java";Because char has a numeric representation, arithmetic can also be performed on it.
char letter = 'A';char next = (char) (letter + 1);
System.out.println(next);This produces B.
Escape Sequences
Java provides escape sequences for characters that are difficult or impossible to write directly.
| Escape | Meaning |
|---|---|
| \n | New line |
| \t | Tab |
| \r | Carriage return |
| \ | Backslash |
| ' | Single quotation mark |
| " | Double quotation mark |
For example:
System.out.println("Java\nProgramming");The output appears on two lines.
The boolean Type
The boolean type represents a logical condition and has only two possible values: true and false. It is commonly used for conditions and flags such as whether a product is available or whether an order has been completed.
boolean inStock = true;boolean orderCompleted = false;Java does not automatically treat numbers or strings as boolean values. For example, 0 is not automatically considered false, and an empty String is not automatically considered false.
Default Values
Primitive fields receive default values when they are declared without an explicit initial value. Numeric types receive zero, boolean receives false, and char receives the null character.
| Type | Default Value |
|---|---|
| byte | 0 |
| short | 0 |
| int | 0 |
| long | 0L |
| float | 0.0f |
| double | 0.0 |
| char | null character |
| boolean | false |
Local variables behave differently. Java does not automatically initialize local variables with default values. A local variable must be assigned a value before it is read.
int quantity;quantity = 10;
System.out.println(quantity);Trying to read quantity before assigning a value causes a compilation error.
Integer Overflow
Integer types have fixed ranges. When an integer calculation exceeds the maximum value that the type can represent, the value can wrap around to the opposite end of the range.
int max = Integer.MAX_VALUE;int result = max + 1;
System.out.println(result);The result becomes the minimum int value instead of producing a larger number. This is important when performing calculations involving large quantities.
When a calculation can exceed the range of int, use long and make sure the calculation itself is performed using long.
long total = (long) quantity * price;The conversion happens before multiplication, so the calculation uses the larger numeric range.
Choosing the Right Primitive Type
For most whole-number values, int is the natural choice. Use long when the required value can exceed the range of int. byte and short are useful in specific memory-sensitive situations, especially when working with large arrays.
For general decimal calculations, double is normally preferred over float. For exact monetary calculations, avoid floating-point types and use an appropriate decimal representation.
Use char when working with individual characters and boolean when representing a true or false condition.
Summary
Java provides eight primitive types: byte, short, int, long, float, double, char, and boolean. Integer types handle whole numbers, floating-point types handle fractional values, char represents a UTF-16 code unit, and boolean represents logical values. Understanding primitive types is essential because they are used throughout Java for variables, calculations, conditions, method parameters, arrays, and many other programming tasks.