Data Types in Java

java

Data types are used to categories the data concerning the type of data. Data types are divided into two groups.

  • Primitive data types
  • Non-primitive data types

Primitive data types

Primitive data types specifies the type and size of variable value. It does not have any additional method.

There are eight primitive data types.

Data TypesSizeDescriptionDefault Value
byte1 byteStores whole numbers from -128 to 1270
short2 bytesStores whole numbers from -32,768 to 32,7670
int4 bytesStores whole numbers from -2,147,483,648 to 2,147,483,6470
long8 bytesStores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,8070L
float4 bytesStores fractional numbers. Sufficient for storing 6 to 7 decimal digits0.0f
double8 bytesStores fractional numbers. Sufficient for storing 15 decimal digits0.0d
boolean1 bitStores true or false valuesfalse
char2 bytesStores a single character/letter or ASCII values‘\u0000’

Byte Data Type

The byte data type is 8 bit signed 2’s complement number. It can store numbers from -128 to 127. Its default value is 0. The byte data type is used to save memory space. When you are sure that number will be in between -128 to 127 that time byte data type is used instead of an integer as a byte is 4 times smaller than an integer.

Example
byte var= 105;
System.out.println(var);

Short Data Type

The short data type is 16 bit signed 2’s complement number. It can store numbers from -32,768 to 32,767. Its default value is 0. It is also used to save memory just like a byte data type. It is 2 times smaller than an integer data type.

Example
short var= 3000;
System.out.println(var);

Int Data Type

The int data type is 32 bit signed 2’s complement number. It can store numbers from -2,147,483,648 to 2,147,483,647. Its default value is 0. Generally, the int data type is used as the default data type for storing integer numbers.

Example
int var= 200000;
System.out.println(var);

Long Data Type

The long data type is 64 bit signed 2’s complement number. It can store numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Its default value is 0. The long data type is used when you want to store the number between range which is more than the range provided by an integer.

Example
long var = 100000L;
System.out.println(var);

Float Data Type

The float data type is a 32-bit floating-point. The float data type is used to store floating-point numbers i.e. decimal number. It is sufficient for storing 6 to 7 decimal digits. Its default value is 0.0F.

Example
float var = 12.5f;
System.out.println(var);

Double Data Type

The double data type is a 64-bit floating-point. It is used to store decimal numbers just like a float data type. It is sufficient for storing 15 decimal digits.

Example
double var = 20.5d;
System.out.println(var);

Boolean Data Type

The boolean data type can store only two values, i.e. true or false. It is used to make flags or checking conditions. Its default value is false.

Example
boolean isTrue= true;
System.out.println(isTrue); 

Char Data Type

The char data type is used to store a single character. It is a single 16-bit character. The character must be inside single quotes for example ‘X’. Its default value is ‘\u0000’.

Example
char option= 'C';
System.out.println(option);

Non-primitive data type

The non-primitive data types are created by the programmer. The non-primitive data types are called reference types because they refer to object. They include Classes, Arrays, Interfaces.

1 thought on “Data Types in Java”

  1. Pingback: Type Casting In Java - TECHNOCODE

Leave a Comment

Your email address will not be published. Required fields are marked *