Primitive or Fundamental Data Types

Learn about fundamental data types.

Integer data type

The integer data type comprises all positive and negative whole numbers. We use the int keyword to define the integer data type. A variable of int type is allocated 4 bytes of memory. It can store any value from -231 to 231-1.

int integer_number = 100;

💡Do you know? If you store 100.5 in a variable of integer type, it would be truncated to 100.

Floating-point data type

The floating-point data type contains a number with a fractional part. We use the float keyword to define the floating-point data type. A variable of a float type is allocated 4 bytes of memory. It can store any value in the range of ±3.40±3.40 x 10±3810^{±38}.

float float_number = 10.7;

Double data type

The double data type contains the number with the fractional part. We use the double keyword to define the double data type. A variable of double type is allocated 8 bytes of memory. It can store any value in the range of ±1.79±1.79 x 10±30810^{±308}.

double double_number = 10.65417;

Difference between float and double

The precision of a floating-point number is the number of digits that can be stored after a decimal point. A float can store seven digits after a decimal point precisely. Whereas, double can store 15 digits after a decimal point precisely. It is recommended to use double for floating-point values.

📝Note: We can store a scientific number in double or float data types. The number after e shows the power of 10.

Example program

Run the code below and see the output!

Get hands-on with 1300+ tech skills courses.