Java Primitive Data Types, What you need to know.
Cheatsheet: Sizes, Ranges, and Default Values
In Java, there are eight primitive data types that can be used to represent different kinds of data. In this article, we will discuss each of them and provide examples of when to use them.
boolean
The boolean data type is used to represent a logical value of either true or false. It is commonly used in conditional statements, loops, and other logical operations. Its size isn’t something that’s precisely defined
Example:
boolean isRaining = true;
boolean isSunny = false;byte
The byte data type is an 8-bit signed two's complement integer and is used to represent whole numbers between -128 and 127. This data type is often used for small integer values, especially when working with streams of data.
Example:
byte myByte = 10;short
The short data type is a 16-bit signed two's complement integer and is used to represent whole numbers between -32,768 and 32,767. This data type is used for integer values that require a larger range than what `byte` can provide but less than what `int` can provide.
Example:
short temperature = -20;int
The int data type is a 32-bit signed two's complement integer and is used to represent whole numbers between -2,147,483,648 and 2,147,483,647. This data type is commonly used for integer values that do not require a large range or high precision. In Java, int is the default data type for integer values.
Example:
int age = 25;
int quantity = 10;long
The long data type is a 64-bit two's complement integer and is used to represent whole numbers between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807. This data type is used for integer values that require a larger range than what `int` can provide. The long data type should be used when int is not enough to represent the required values.
Example:
long population = 7_594_000_000L;
long distanceToSun = 149_600_000L;Note that the L character at the end of the number indicates that the value is a long.
float
The float data type is a single-precision 32-bit IEEE 754 floating point and is used to represent decimal numbers with a range of approximately -3.4E38 to 3.4E38 and a precision of 6–7 digits. This data type is used for values that require less precision than what double provides or when memory is a concern.
Example:
float weight = 65.5f;
float temperature = 25.7f;Note that the f character at the end of the number indicates that the value is a float.
double
The double data type is a double-precision 64-bit IEEE 754 floating point and is used to represent decimal numbers with a higher precision than what float can provide. This data type is commonly used for scientific and mathematical calculations that require high accuracy.
Example:
double pi = 3.141592653589793;
double e = 2.718281828459045;char
The char data type is a single 16-bit Unicode character and is used to represent a single character.
Example:
char firstLetter = 'a';
char lastLetter = 'z';It can also be used for numerical values between 0 and 65,535. It does not support negative values, making it effectively an unsigned data type.
// Parsing an unsigned short
short s = (short) Integer.parseInt("64000");
System.out.println(Short.toUnsignedInt(s)); // 64000Default values
In Java, if a primitive data type is declared as an instance variable, it will have a default value assigned to it automatically. The default values for each primitive data type are:
boolean:falsebyte:0short:0int:0long:0Lfloat:0.0fdouble:0.0dchar:\u0000(null character)
It is not necessary to assign a default value to a primitive data type, as Java will do it automatically.
Conclusion
Choosing the right data type in Java is important for efficient memory usage and accurate calculations. In summary, use boolean for logical values, byte for small integer values, short for integer values with a larger range, int for integer values that do not require a large range or high.
Thanks for reading
- 👏 Please clap for the story (50 claps) to help the article to be spread
- 🌐 Share the story on Social Media
- ➕More stories about Programming, Career, AI and more.
- 🔔 Follow me: Medium | LinkedIn | Twitter
- 📝 Join the Medium Membership Program for only 5$ a month, and support me and other writers to keep on the tremedous work.






