Primitive Types in Java
In Java, primitive types are the most basic data types that are not objects. They are predefined by the language and named by a reserved keyword. Java has eight primitive data types:
- Integer types: byte, short, int (default), long
- Floating-point types: float, double (default)
- Character type: char
- Boolean type: boolean
Details
- int - 32-bit signed two's complement integer. Default type for integer values.
- float - 32-bit floating point. Used to save memory in large arrays of floating point numbers. Not recommended for precise values.
- long - 64-bit signed two's complement integer. Use 'L' suffix to denote long literals.
- double - 64-bit floating point. Default type for decimal values. More precise than float.
Literals in Java
Literals are the fixed values assigned to variables in Java. They represent data directly in the source code. Java supports several types of literals corresponding to its primitive data types:
- Integer Literals: Used for
byte,short,int, andlongtypes. By default, integer literals are of typeint. To specify alongliteral, append an 'L' or 'l' to the number (e.g.,100L). Don't use 'l' as it can be confused with the digit '1'. - Floating-Point Literals: Used for
floatanddoubletypes. By default, floating-point literals are of typedouble. To specify afloatliteral, append an 'F' or 'f' to the number (e.g.,10.5f). - Character Literals: Used for the
chartype. Character literals are enclosed in single quotes (e.g.,'A','1','\n'). - Boolean Literals: Used for the
booleantype. The only two boolean literals aretrueandfalse. - String Literals: Although not a primitive type, string literals are sequences of characters enclosed in double quotes (e.g.,
"Hello, World!").
Examples of Primitive Type Declarations
PrimitiveTypes.java
byte byteValue = 100; // 8-bit signed integer
short shortValue = 10000; // 16-bit signed integer
int age = 25; // 32-bit signed integer
int count = 1_000_000; // 32-bit signed integer with underscores for readability
int hexValue = 0x1A; // Hexadecimal literal
int binValue = 0b1010; // Binary literal
long longValue = 100000L; // 64-bit signed integer
float floatValue = 10.5f; // 32-bit floating point
double price = 20.99; // 64-bit floating point
double scientific = 1.23e4; // Scientific notation
double coeff = 23_456.75_01; // 64-bit floating point with underscores
double likeInt = 100D; // 64-bit floating point with 'D' suffix
char a = 'A'; // 16-bit Unicode character
boolean isActive = true; // boolean value
String greeting = "Hello, World!"; // String literal - not a primitive type
Here is a summary table of the primitive types in Java
| Type | Size | Range | Description |
|---|---|---|---|
| byte | 8 bits | -128 to 127 | Used to save memory in large arrays. |
| short | 16 bits | -32,768 to 32,767 | Also used to save memory in large arrays, but has a larger range than byte. |
| int | 32 bits | -2,147,483,648 to 2,147,483,647 | The default data type for integer values. |
| long | 64 bits | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | Used when a wider range than int is needed. |
| float | 32 bits | Approximately ±3.40282347E+38F (6-7 significant decimal digits) | Used to save memory in large arrays of floating point numbers. Not recommended for precise values. |
| double | 64 bits | Approximately ±1.79769313486231570E+308 (15 significant decimal digits) | The default data type for decimal values. More precise than float. But use BigDecimal for precise values like healthcare and banking. |
| char | 16 bits | '\u0000' (0) to '\uffff' (65,535) | Used to store a single 16-bit Unicode character. |
| boolean | true or false | Used for simple flags that track true/false conditions. |
Key Nuances to Remember
- The
charis Unsigned: Unlike the integer types (byte,short,int,long) which are signed (allowing negative numbers), thechartype is unsigned. It ranges from to 0 to 65,535. - Boolean Size: The JVM usually uses 1 byte to represent a
booleanand a 4-byte (word) forbooleanarrays. It is conceptually 1 bit, but memory is addressed in bytes. - Integer Overflow: If you add 1 to the maximum value of an
int(2,147,483,647), it wraps around to the minimum value (-2,147,483,648). This is due to the two's complement representation. - Primitive vs. Wrapper Classes: For every primitive type, Java provides a corresponding Wrapper Class (e.g.,
inthasInteger,charhasCharacter). You’ll need these when working with Collections likeArrayListorHashMap, which cannot store primitives directly.