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;
- Create a Zip file using Java Code programmatically
- Eclipse : A java Runtime Environment (JRE) or Java Development kit (JDK) must be available
- How to Sort a LinkedList in Java
- Loading class com.mysql.jdbc.Driver. This is deprecated. The new driver class is com.mysql.cj.jdbc.Driver
- How to declare and initialize Array in Java Programming
- [Fix] java: integer number too large compilation error
- Java JDBC Connection with MySQL Driver in VS Code + Troubleshooting
- Reading .xls and .xlsx Excel file using Apache POI Java Library
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- How to get Client IP address using Java Code Example
- Truncate table using Java JDBC Example
- Struts 2 : There is no Action mapped for namespace [/] and action name [form] associated with context path [/proj]
- How to get file path in Idea IntelliJ IDE
- Java Generics explained with simple definition and examples
- Java SE 8 Update 301 available with various bug fixes and security improvements
- Java: Collect Stream as ArrayList or LinkedList
- Java JDBC Connection with PostgreSQL Driver Example
- How to check if Java main thread is alive
- How to fix Java nio NoSuchFileException wile reading a file
- Java 8+ get Day of the Week Examples with LocalDateTime, DateTime, ZonalDateTime and Instant classes
- Ways to Convert Integer or int to Long in Java
- [Java] How to throws Exception using Functional Interface and Lambda code
- [Fix] Spring Boot: mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
- Java: The value of the local variable string is not used
- Java JDBC: Insert Java 8 LocalDate and Time using PreparedStatement
- Change the default download location for Mac Safari - MacOS
- Text Case Converter - Convert to Upper/Lower/Proper/Random Case - Tools
- NOTE: This project contains resource errors, so aapt did not succeed, which can cause rendering failures. Fix resource problems first. - Android
- Fix - 412 Cookies Are Disabled error in Exchange Admin Center when you click hybrid - Microsoft
- Use your iPhone microphone as a mic on macOS Ventura - MacOS
- [Fix] Microsoft Teams a JavaScript error occurred in the main process Error - Teams
- How to change Android Titlebar theme color from Purple - Android
- Convert SQL to CSV in Notepad++ - NotepadPlusPlus
Diagram
