Dark Mode On/Off

# Java Primitives

A primitive data type is a basic data type provided by a programming language. Java contains eight primitive data. Unlike objects, primitive types are directly stored on the stack memory and not in the heap memory. The following image illustrates how the primitive data types are categorized.

## Java int Primitive Data Type

The int data type(short for integer) is used to store integer values. It uses 32 bits of memory and so the values can range from -2,147,483,648 (-2^31) to 2,147,483,647 (2^31-1).

Different Arithmetic operations are performed on int data types. Remember that if we perform arithmetic operations on decimals and store it in an int-type variable, then the decimal part of the result is lost. The int data type takes a default value of 0 if no value has been assigned.

``int i = 5;``

## Java byte Primitive Data Type

The byte data type is also used to store integers, but it can take only 8 bits of space(hence the name byte). The range of the values is from -128 to +127, and the default value is zero.

``byte b = 5;``

## Java short Primitive Data Type

The short is also an integer data type and uses 16 bits of memory to store integers. The values can range from -32,768(-2^15) to 32,767(2^15 – 1). As you may have guessed, the default value for short is also zero.

``short s = 5;``

## Java long Primitive Data Type

If we need more space, then the long data type is a perfect fit. It uses 64 bits of memory to store memory. The values can range from -2^63 to 2^63 - 1. Just like other integer data types, its default value is zero. It can work with all standard arithmetic operators.

``long l = 5;``

## Java float Primitive Data Type

The float data type is used to store and work with decimal-point values. Float is a single-precision data type. It means that the values become less precise after six decimal places.

It is recommended to use other decimal classes like `BIgDecimal` if precision is an issue. It uses 32 bits of memory. The values can range from 1.40239846 x 10^-45 to 3.40282347 x 10^38. This range can be positive or negative. Its default value is 0.0. We need to specify the letter 'f' while assigning a value to a float data type variable. We need to do this because double is the default type for decimals.

``float f = 5.0f;``

## Java double Primitive Data Type

The double type is used to store decimals, just like float. However, it uses 64 bits of memory instead of 32 bits. Unlike float, double uses double-precision and so there is no limitation of precision. The values can range from 4.9406564584124654 x 10^-324 to 1.7976931348623157 x 10^308. This range can be positive or negative. Its default value is 0.0.

``double d = 5.0;``

## Java boolean Primitive Data Type

The boolean data type belongs to a different category. It is used to store just two values, true or false. Just a single bit is required to store these values, but Java pads the value and stores it as one byte. Its default value is false. This type is very commonly used in control-flow statements.

``````boolean t = true;
boolean f = false;``````

## Java char Primitive Data Type

The char data type uses 16 bits of memory to store Unicode characters. Its values can range from 0 to 65535. In Unicode, this range would be from \u0000 to \uffff. The default value for the char data type is 0 or \u0000.

``````public static void main(String[] args)
{
char c1 = 67;
char c2 = 'C';
System.out.println(c1);
System.out.println(c2);
System.out.println(c1 + c2);
}``````

C
C
134

## Overflow and Underflow in Primitive Types

Overflow occurs when a value exceeds its maximum defined range value. Similarly, underflow will occur if the value becomes greater than the lowest range value specified.

In Java, integers and decimals may overflow or underflow if we assign values that are not in their range.

For integers, when overflow occurs, then the value rolls over to the minimum value, and counting will begin from there.

``````public static void main(String[] args)
{
int i = Integer.MAX_VALUE;
System.out.println(i);
i = i + 1;
System.out.println(i);
}``````

2147483647
-2147483648

Similarly, when the underflow of integers occurs then the value rolls over to the positive extreme.

``````public static void main(String[] args)
{
int i = Integer.MIN_VALUE;
System.out.println(i);
i = i - 1;
System.out.println(i);
}``````

-2147483648
2147483647

## Autoboxing in Java

Java has a wrapper class for each primitive type. These wrapper classes are generally used with generics. We don't need to do anything special to convert primitives to objects of the wrapper class. Java automatically handles this conversion, and this is known as Autoboxing.

``````public static void main(String[] args)
{
Integer i = 5;
Byte b = 5;
Short s = 5;
Long l = 5L;//Letter L is used as the default type for numbers is int
Character c = 'C';
Double d = 5.0;
Float f = 5.0f;
Boolean bool = true;
}``````

## Summary

In this tutorial, we discussed the eight basic, primitive data types present in Java. The following table summarizes the key points discussed.

 Primitive Type Size(Bits) Range Default int 32 -2^31 to (2^31)-1 0 byte 8 -2^7 to (2^7)-1 0 short 16 -2^15 to (2^15)-1 0 long 64 -2^63 to (2^63)-1 0 float 32 up to 7 decimal places 0.0f double 64 up to 16 decimal places 0.0 boolean 1 true or false false char 16 0 to (2^16)-1 \u0000

Want to learn coding?
Try our new interactive courses.
Over 20,000+ students enrolled.