There are 8 primitive data types in Java Programming Language.
byte
short
int
float
long
double
char
boolean
Note: The primitive data types names are case-sensitive and represented in lower cases. They are also among the reserved keywords in Java and hence cannot be used to define variables of any kind.
If you find it hard to remember them you can remember them using a mnemonic:
"Big Surfers In Los Angeles Love Finding Dolphins, Sharks, and Barracudas"
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".
Primitives are the most basic building blocks of data and are used to define more complex data types or structures.
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
int is the default data type in numeric values in Java.
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.
A prefix of f or F is required to represent a float value.
Examples:
float amount = 450.25f;
float latitude = 34.23F;
float pi = 3.1415f;
long
A prefix of l or L is required to represent a float value.
Used when int is not sufficient to store a value.
Examples:
long populationOfCity = 7794798739L;
long distanceToSun = 149600000000l;
long numberOfAtomsInUniverse = 10000000000000000000L;
double
double is the default data type in decimal values.
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;
Diagram
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!
Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
Thanks for your feedback! If you have time, please provide details by selecting options below.
😊 Thanks for your time, your feedback has been registered!
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!