There are 8 primitive data types in Java Programming Language.
- byte
- short
- int
- float
- long
- double
- char
- boolean
Why are they called "Primitives"?
Back in the days when computers were invented they were mainly meant to perform mathematical calculations. The initial programming languages (such as FORTRAN and COBOL) were limited to integers, real numbers, and characters. As computers become more powerful (more CPU, RAM, and memory) more complex features, such as data structures, functions, and object orientation got incorporated into programming.
As in Java Programming int, boolean, double, etc. can be represented as both objects (Integer, Boolean, Double e.t.c) and in the traditional way, hence we call them "primitive data types".
Range information about Primitives
Primitive Data Type | Range | Bytes |
---|---|---|
byte | -128 to 127 | 1 |
short | -32,768 to 32,767 | 2 |
int | -2,147,483,648 to 2,147,483,647 | 4 |
float | 3.4028235E38 to 1.4E-45 (approx) | 4 |
long | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | 8 |
double | 1.7976E308 to 4.9E-324 (approx) | 8 |
char | 0 to 65,535 (unsigned) | 2 |
boolean | true or false | 1 |
Primitives Examples:
- byte
One should make use of byte when you know the range of the value is below -128 to 127, or when the storage is limited.
Examples:
byte daysInAWeek = 7; byte daysInAMarch = 31; byte age = 25; byte temperature = -10;
- short
One should make use of short when you know the range of the value is below -32,768 to 32,767, or when the storage is limited.
Examples:
short daysInaYear = 365; short distance = 1000; short temperature = -273;
- int
It is widely used to represent integer values based on the vast range.
Examples:
int amount = 4500000; int counter = 54050;
- float
floats are used to represent decimal values.
Examples:
float amount = 450.25f; float latitude = 34.23F; float pi = 3.1415f;
- long
Used when int is not sufficient to store a value.
Examples:
long populationOfCity = 7794798739L; long distanceToSun = 149600000000l; long numberOfAtomsInUniverse = 10000000000000000000L;
- double
Used when the decimal values need high precision, or float is not sufficient to store a value.
Examples:
double amount = 4500000.556545; double pi = 3.14159265358979323846264338327950288419716939937;
- char
char's are unsigned and are used to represent single character. Examples: an alphabet, digit, symbol, or Unicode characters.
Examples:
char smiley = '\u263A'; char letterA = 'A';
- boolean
It is a logical data type that can have only two values true or false (in lowercase), used to store a result of the comparison.
Examples:
boolean isHoliday = false; boolean flag = true;
- Get the current timestamp in Java
- Java Stream with Multiple Filters Example
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Fix: UnsupportedClassVersionError: Unsupported major.minor version 63.0
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- 7 deadly java.lang.OutOfMemoryError in Java Programming
- How to Calculate the SHA Hash Value of a File in Java
- Java JDBC Connection with Database using SSL (https) URL
- How to Add/Subtract Days to the Current Date in Java
- Create Nested Directories using Java Code
- Spring Boot: JDBCTemplate BatchUpdate Update Query Example
- What is CA FE BA BE 00 00 00 3D in Java Class Bytecode
- Save Java Object as JSON file using Jackson Library
- Adding Custom ASCII Text Banner in Spring Boot Application
- [Fix] Java: Type argument cannot be of primitive type generics
- List of New Features in Java 11 (JEPs)
- Java: How to Add two Maps with example
- Java JDBC Transition Management using PreparedStatement Examples
- Understanding and Handling NullPointerException in Java: Tips and Tricks for Effective Debugging
- Steps of working with Stored Procedures using JDBCTemplate Spring Boot
- Java 8 java.util.Function and BiFunction Examples
- The Motivation Behind Generics in Java Programming
- Get Current Local Date and Time using Java 8 DateTime API
- Java: Convert Char to ASCII
- Deep Dive: Why avoid java.util.Date and Calendar Classes
- [fix] Cannot connect to the Docker daemon at unix:var/run/docker.sock. Is the docker daemon running? - Docker
- How to Restart Apple Silicon based Mac (M1/M2 Chip) in Safe Mode - MacOS
- Python: Pandas Merge Indicator (Left, Right and Both) Example - Python
- [Android] RuntimeException: Unable to start activity Need BLUETOOTH permission - Android
- How to delete a dir or folder using Python code - Python
- Visual Studio Code: Restricted Mode is intended for safe code browsing - MacOS
- cURL -x Use of HTTP Proxy Server Examples - cURL
- Bash Command To Check If File Exists - Bash