Primitive Data Types in Java

Primitive data types in Java

Primitive data types in Java are built-in data types that are available in the language from the beginning. In this blog post, we’ll take a look at the different primitive data types in Java and their range.

int data type

The int data type we use to store whole numbers (integers).

Example:

int rollNumber  = 5;

In the above code, we declare a variable called rollNumber to store a numeric value 5. The int data type can store a value size of 4 bytes and can range from -2147483648 to 2147483647.

long data type

This data type is also used to store whole numbers, but it has a larger size of 8 bytes and can store values in the range from -9223372036854775808 to 9223372036854775807.

Example:

long mobileNumber = 9851150050;

short data type

This data type is similar to int, but it has a smaller size of 2 bytes and can store values in the range of -32768 to 32767.

byte data type

This data type is similar to short, but it has an even smaller size of 1 byte and can store values in the range of -128 to 127.

float data type

The float data type we use to store floating-point numbers (numbers with decimal points).

Example:

float price = 125.50f;

In the above code, I’ve added a character f along with the value. If we don’t define a value along with the f character then the Java compiler will assume it as a double value. Hence, it is very important to remember this while declaring a float value.

It has a size of 4 bytes and can store values in the range of approximately 3.4 x 10^-38 to 3.4 x 10^38.

double data type

This data type is similar to float, but it has a larger size of 8 bytes and can store values in the range of approximately 1.7 x 10^-308 to 1.7 x 10^308.

char data type

The char data type we can use to store a single character enclosing by a single quote, such as a letter or a symbol. It has a size of 2 bytes and can store any Unicode character.

Example:

char gender = 'M';

boolean data type

This data type is used to store a true or false value. It has a size of 1 byte and can only store the values true or false.

Example:

boolean isMale = true;

These primitive data types in Java are essential to work with, as they are the basic building blocks for storing and manipulating data. When writing code in Java, it’s important to choose the appropriate primitive data type based on the type of data we need to store.

These data types have assigned a default value. To understand the default values of each data type, you can see below table:

Data typeDefault value
byte0
short0
int0
long0L
float0.0f
double0.0d
char‘\u0000’
booleanfalse
Primitive Types Default Values

In summary, the primitive data types in Java include int, long, short, byte, float, double, char, and boolean. We can use these data types to store a variety of different types of data and are an important part of working with the Java programming language.

Frequently Asked Questions

What are primitive data types in Java?

Primitive data types in Java are basic data types used to represent simple values like integers, floating-point numbers, characters, and booleans.

How many primitive data types are there in Java?

Java has 8 primitive data types, including byte, short, int, long, float, double, char, and boolean.

What is the difference between primitive and reference data types in Java?

Primitive data types store actual values, while reference data types (objects) store references to objects in memory.

What is the default value for primitive data types in Java?

Primitive data types have default values, such as 0 for numeric types, false for boolean, and ‘\u0000’ for char, when not explicitly initialized.

Can I create user-defined data types in Java similar to primitives?

No, Java doesn’t allow users to create custom primitive data types; you can only create objects and use reference data types for that purpose.