28
AugPrimitive Data Types in Java
Primitive data types in Java are the fundamental data types provided by the language to represent simple values directly in memory, such as integers, floating-point numbers, characters, and Boolean values. They are not objects and are used to perform basic operations with high performance and minimal memory usage.
For more tips and guidance to learn Java Programming, try our Free Java Certification Course.
Java Primitive Data Types
Read More -
1. Boolean Data Type
boolean variableName;
Example
public class BooleanExample {
public static void main(String[] args) {
boolean isJavaFun = true;
boolean isCodingHard = false;
System.out.println("Is Java fun? " + isJavaFun);
System.out.println("Is coding hard? " + isCodingHard);
if (isJavaFun) {
System.out.println("Yes, Java is fun!");
} else {
System.out.println("No, Java is not fun.");
}
if (isCodingHard) {
System.out.println("Coding is hard.");
} else {
System.out.println("Coding is not hard.");
}
}
}
Output
Is Java fun? true
Is coding hard? false
Yes, Java is fun!
Coding is not hard.
2. Byte Data Type
byte variableName;
Example
public class ByteExample {
public static void main(String[] args) {
byte numberOfStudents = 20;
byte numberOfTeachers = 3;
System.out.println("Number of students: " + numberOfStudents);
System.out.println("Number of teachers: " + numberOfTeachers);
byte totalPeople = (byte) (numberOfStudents + numberOfTeachers);
System.out.println("Total number of people: " + totalPeople);
// Byte data type ranges from -128 to 127
byte valueOutOfRange = (byte) 150;
System.out.println("Value out of range: " + valueOutOfRange);
}
}
Output
Number of students: 20
Number of teachers: 3
Total number of people: 23
Value out of range: -106
3. Char Data Type
Syntax
char variableName;
Example
public class CharExample {
public static void main(String[] args) {
char firstLetter = 'A';
char secondLetter = 'B';
System.out.println("First letter: " + firstLetter);
System.out.println("Second letter: " + secondLetter);
// Char can also hold Unicode characters
char unicodeChar = '\u03A9'; // Greek capital letter omega
System.out.println("Unicode character: " + unicodeChar);
// Char can be used in arithmetic operations (as it's internally represented as
// numbers)
char result = (char) (firstLetter + 1);
System.out.println("Next letter: " + result);
}
}
Output
First letter: A
Second letter: B
Unicode character: Ω
Next letter: B
4. Short Data Type
The short data type in Java is a 16-bit signed two’s complement integer that ranges from -32,768 to 32,767. Like the byte data type, it is useful for saving memory in large arrays when memory optimization is required. The default value of a short variable is 0
Syntax
short variableName;
Example
public class ShortExample {
public static void main(String[] args) {
short temperatureToday = 25;
short numberOfStudents = 150;
System.out.println("Temperature today: " + temperatureToday + " degrees Celsius");
System.out.println("Number of students: " + numberOfStudents);
short totalNumberOfPeople = (short) (numberOfStudents + 10);
System.out.println("Total number of people: " + totalNumberOfPeople);
// Short data type ranges from -32,768 to 32,767
short valueOutOfRange = (short) 35000; // Corrected to use short and added casting
System.out.println("Value out of range: " + valueOutOfRange);
}
}
On executing the above code in the Java Playground, we'll get the below output.
Output
Temperature today: 25 degrees Celsius
Number of students: 150
Total number of people: 160
Value out of range: -30536
5. Int Data Type
Syntax
int variableName;
Example
public class IntExample {
public static void main(String[] args) {
int numberOfStudents = 100;
int numberOfTeachers = 10;
System.out.println("Number of students: " + numberOfStudents);
System.out.println("Number of teachers: " + numberOfTeachers);
int totalPeople = numberOfStudents + numberOfTeachers;
System.out.println("Total number of people: " + totalPeople);
// Integers can hold very large values
int bigValue = 2147483647;
System.out.println("Big value: " + bigValue);
// Integers overflow if the value exceeds the range
int overflowValue = 2147483647 + 1;
System.out.println("Overflow value: " + overflowValue);
}
}
Output
Number of students: 100
Number of teachers: 10
Total number of people: 110
Big value: 2147483647
Overflow value: -2147483648
6. Long Data Type
The long data type in Java is a 64-bit signed two’s complement integer used when a wider range than int is required. It is suitable for storing very large numeric values and has a range from −2⁶³ (−9,223,372,036,854,775,808) to 2⁶³−1 (9,223,372,036,854,775,807). The default value of a long variable is 0L, and long literals should be suffixed with an L or l.
Syntax
long variableName;
Example
public class LongExample {
public static void main(String[] args) {
long populationOfCountry = 1500000000L; // Note the 'L' suffix for long literals
long distanceToMoon = 384400000L;
System.out.println("Population of the country: " + populationOfCountry);
System.out.println("Distance to the moon (in meters): " + distanceToMoon);
long totalDistance = populationOfCountry * distanceToMoon;
System.out.println("Total distance (population x distance to moon): " + totalDistance);
// Long data type can hold even larger values
long bigValue = 9223372036854775807L;
System.out.println("Big value: " + bigValue);
// Long data type can also represent negative values
long negativeValue = -1000000000L;
System.out.println("Negative value: " + negativeValue);
}
}
Output
Population of the country: 1500000000
Distance to the moon (in meters): 384400000
Total distance (population x distance to moon): 576600000000000000
Big value: 9223372036854775807
Negative value: -1000000000
7. Float Data Type
The float data type in Java is a 32-bit single-precision IEEE 754 floating-point number used to store decimal or fractional values. It is suitable for saving memory in large arrays of floating-point numbers where precision is not a major concern. The default value of a float variable is 0.0f, and float literals must be suffixed with an f or F.
Syntax
float variableName;
Example
public class FloatExample {
public static void main(String[] args) {
float temperatureToday = 25.5f; // Note the 'f' suffix for float literals
float distanceToDestination = 123.45f;
System.out.println("Temperature today: " + temperatureToday + " degrees Celsius");
System.out.println("Distance to destination: " + distanceToDestination + " kilometers");
float totalDistance = temperatureToday + distanceToDestination;
System.out.println("Total distance: " + totalDistance + " units");
// Float data type can represent very large or very small values
float bigValue = 3.4028235E38f; // Maximum positive value for float
float smallValue = 1.4E-45f; // Minimum positive value for float
System.out.println("Maximum positive value for float: " + bigValue);
System.out.println("Minimum positive value for float: " + smallValue);
}
}
Output
Temperature today: 25.5 degrees Celsius
Distance to destination: 123.45 kilometers
Total distance: 148.95 units
Maximum positive value for float: 3.4028235E38
Minimum positive value for float: 1.4E-45
8. Double Data Type
Syntax
double variableName;
Example
public class DoubleExample {
public static void main(String[] args) {
double temperatureToday = 25.5;
double distanceToDestination = 123.45;
System.out.println("Temperature today: " + temperatureToday + " degrees Celsius");
System.out.println("Distance to destination: " + distanceToDestination + " kilometers");
double totalDistance = temperatureToday + distanceToDestination;
System.out.println("Total distance: " + totalDistance + " units");
// Double data type can represent very large or very small values
double bigValue = 1.7976931348623157E308; // Maximum positive value for double
double smallValue = 4.9E-324; // Minimum positive value for double
System.out.println("Maximum positive value for double: " + bigValue);
System.out.println("Minimum positive value for double: " + smallValue);
}
}
Output
Temperature today: 25.5 degrees Celsius
Distance to destination: 123.45 kilometers
Total distance: 148.95 units
Maximum positive value for double: 1.7976931348623157E308
Minimum positive value for double: 4.9E-324
Importance of Primitive Data Types
Primitive data types are fundamental to Java programming. They provide the most basic forms of data representation and are essential for writing efficient, clear, and high-performance code. Below are the key reasons why primitive data types are important:
1. Basic Building Blocks of Data- Primitive data types serve as the foundation for data manipulation in Java. They allow developers to represent simple values such as numbers, characters, and logical states directly in memory.
2. High Performance and Speed- Operations involving primitive types are executed faster than those involving objects. Since they store values directly in memory (rather than references), they eliminate the overhead associated with object creation and access.
3. Memory Efficiency- Primitive types are memory-efficient, especially when used in large arrays or memory-critical applications. For example, using byte or short instead of int can reduce memory usage significantly in certain scenarios.
4. Simplified Programming- Primitive data types make coding simpler and more straightforward. They are easy to declare and use, making them ideal for performing basic arithmetic, logical, and control flow operations.
5. Essential for Control Structures- Control statements in Java—such as if, while, for, and switch—rely heavily on primitive types (especially boolean) to evaluate conditions and manage program flow.
6. Default Initialization- Each primitive type in Java has a default value (e.g., 0 for int, false for boolean), which ensures predictable behavior when variables are declared but not explicitly initialized.
7. Avoids Object Overhead- Unlike wrapper classes (e.g., Integer, Double), primitive types do not include additional methods or metadata, making them lightweight and optimal for performance-sensitive tasks.
List of All 8 Primitive Data Types with a Table:
Data type | Size | Default value | Range | Usage example |
byte | 1 byte | 0 | -128 to 127 | Memory-efficient arrays |
short | 2 bytes | 0 | -32,768 to 32,767 | Small integers |
int | 4 bytes | 0 | -2,147,483,648 to 2,147,483,647 | Default for integers |
long | 8 bytes | 0L | -9 quintillion to +9 quintillion | Large numeric values |
float | 4 bytes | 0.0f | Approx. ±3.4e38 | Low-precision decimals |
double | 8 bytes | 0.0d | Approx. ±1.8e308 | High-precision decimals |
char | 2 bytes | '\u0000' | 0 to 65,535 (Unicode characters) | Single characters |
Boolean | 1 byte | false | true or false | Logic/conditions |
Summary
FAQs
int number = 10;
Take our Java skill challenge to evaluate yourself!

In less than 5 minutes, with our skill challenge, you can identify your knowledge gaps and strengths in a given skill.