Java is a strongly typed language, and it provides eight primitive data types to represent simple values such as numbers, characters, and logical values. Primitive data types are predefined by the language and serve as the building blocks for data manipulation.
Categories of Primitive Data Types
- Numeric Data Types:
- For storing numbers (integer and floating-point values).
- Non-Numeric Data Types:
- For characters and logical values.
Category | Data Type | Size | Default Value | Range |
Integer | byte | 8 bits | 0 | -128 to 127 |
short | 16 bits | 0 | -32,768 to 32,767 | |
int | 32 bits | 0 | -2^31 to 2^31 – 1 | |
long | 64 bits | 0L | -2^63 to 2^63 – 1 | |
Floating-Point | float | 32 bits | 0.0f | Approximately ±3.40282347E+38F |
double | 64 bits | 0.0d | Approximately ±1.79769313486231570E+308 | |
Character | char | 16 bits (Unicode) | \u0000 | 0 to 65,535 |
Boolean | boolean | 1 bit (virtual) | false | true or false |
Detailed Overview
1. Integer Types
Integer data types are used to store whole numbers.
- byte:
- Smallest integer type.
- Useful for saving memory in large arrays.
- Example:
byte b = 100;
- short:
- Larger than byte, used when memory is a constraint.
- Example:
short s = 20000;
- int:
- Default type for integers.
- Commonly used in programs.
- Example:
int num = 123456;
- long:
- Larger range than int.
- Use suffix L to denote a long literal.
- Example:
long distance = 12345678910L;
2. Floating-Point Types
Floating-point types store decimal numbers.
- float:
- Single-precision, less accurate but consumes less memory.
- Use suffix f for float literals.
- Example:
float price = 10.99f;
- double:
- Double-precision, more accurate.
- Default for floating-point numbers.
- Example:
double pi = 3.141592653589793;
3. Character Type
- char:
- Stores a single 16-bit Unicode character.
- Enclosed in single quotes.
- Example:
char grade = ‘A’;
4. Boolean Type
- boolean:
- Represents one of two values: true or false.
- Commonly used in conditional statements.
- Example:
boolean isJavaFun = true;
Default Values
Each primitive type has a default value when it is a member of a class (uninitialized local variables must be explicitly initialized).
Data Type | Default Value |
byte | 0 |
short | 0 |
int | 0 |
long | 0L |
float | 0.0f |
double | 0.0d |
char | \u0000 |
boolean | false |
Type Conversion
1. Implicit Conversion
- Automatically performed when converting a smaller type to a larger type.
- Example:
int num = 10;
long bigNum = num; // Implicit conversion
2. Explicit Casting
- Required when converting a larger type to a smaller type.
- Example:
long largeNum = 100L;
int smallNum = (int) largeNum; // Explicit casting
Example Program
public class PrimitiveTypesDemo {
public static void main(String[] args) {
// Integer types
byte smallValue = 10;
short mediumValue = 1000;
int integerValue = 100000;
long largeValue = 100000000L;
// Floating-point types
float price = 19.99f;
double bigDecimal = 12345.6789;
// Character type
char grade = ‘A’;
// Boolean type
boolean isPassed = true;
// Displaying values
System.out.println(“Byte value: ” + smallValue);
System.out.println(“Short value: ” + mediumValue);
System.out.println(“Int value: ” + integerValue);
System.out.println(“Long value: ” + largeValue);
System.out.println(“Float value: ” + price);
System.out.println(“Double value: ” + bigDecimal);
System.out.println(“Char value: ” + grade);
System.out.println(“Boolean value: ” + isPassed);
}
}
Output
Byte value: 10
Short value: 1000
Int value: 100000
Long value: 100000000
Float value: 19.99
Double value: 12345.6789
Char value: A
Boolean value: true
Key Points to Remember
- Size:
- Each type has a fixed size, irrespective of the platform, ensuring portability.
- Efficiency:
- Choosing the correct type improves memory efficiency and performance.
- Default Type:
- int is the default for integer literals.
- double is the default for floating-point literals.
- Overflow:
- Exceeding the range of a type results in an overflow, leading to unpredictable values.
By mastering Java’s primitive data types, developers can handle basic data processing effectively and build robust programs.