Variables and Data-Types in Java

Table of Contents

Variables and Data-Types in Java

In this tutorial we will discuss the Variables and the Data types used in Java.

Variables in Java

By general definition ,variable is something which acquires memory and the value stored into can be changed as required.

For example, in the below code, a is the variable that holds the value of 5 assigned to it.

int  a = 5;

In the above code int is the Data-type of the variable a and 5 is the value assigned to it.

And suppose that you have used it inside a program and again you want to alter the value of it to 10, then it is possible.

Just think of a scenario in which a Bank provides a locker facility. One of the customers requested the branch manager to arrange a locker for him, but unfortunately, no free locker was there. After a couple of days, one of the old customers freed up his locker. Thus the Branch manager contacted the customer with the request of the locker to visit the Branch as the locker is available now.

Here, the locker of the Bank is like a variable and the ornament or paper stored inside the locker is like a value that can be assigned into it on demand.

Data Types in Java

Data types declare the different sizes and types of values that can be assigned in the variable. There are two types of data types in Java:

  1. Primitive data types: The primitive data types include int, char , float, boolean, byte, short, long, and double.
  2. Non-primitive data types: The non-primitive data types include String, Class, Interface, and Array.

Primitive Data Types

Primitive data types are the basic building blocks of data manipulation.

As, Java is a statically-typed programming language which means, all variables must be declared before its use.

There are the primitive data types:

  • int data type
  • char data type
  • float data type
  • short data type
  • byte data type
  • long data type
  • boolean data type
  • double data type
Data TypeSizeDescription
byte1 byteStores whole numbers from -128 to 127
short2 bytesStores whole numbers from -32,768 to 32,767
int4 bytesStores whole numbers from -2,147,483,648 to 2,147,483,647
long8 bytesStores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float4 bytesStores fractional numbers.
char2 bytesStores a single character/letter or ASCII values
double8 bytesStores fractional numbers but double the capacity of a float type
boolean1 bitStores true or false values

Boolean Data Type

It is used to store only two possible values: true and false.

It is used for simple flags that track true/false conditions.

The Boolean data type specifies one bit of information, but its “size” can’t be defined precisely.

Example: Boolean a = false

Byte Data Type

It is an 8-bit signed two’s complement integer. Its value-range lies between -128 to 127 (inclusive). Its minimum value is -128 and the maximum value is 127 whereas its default value is 0.

It saves space because a byte is 4 times smaller than an integer. It can also be used in place of “int” data type.

Example: byte a = 10

Short Data Type

It is a 16-bit signed two’s complement integer. Its value-range lies between -32,768 to 32,767 (inclusive). Its minimum value is -32,768 and the maximum value is 32,767. Although its default value is considered as 0.

The short data type can also be used to save memory.

Example: short s = 10

Int Data Type

It is a 32-bit signed two’s complement integer. Its value-range lies between – 2,147,483,648 (-2^31) to 2,147,483,647 (2^31 -1) (inclusive). Its minimum value is – 2,147,483,648and maximum value is 2,147,483,647. Its default value is considered as 0.

The int data type is generally used as a default data type for any values unless if there is no problem with memory specified to it.

Example: int a = 10

Long Data Type

It is a 64-bit two’s complement integer. Its value-range lies between -9,223,372,036,854,775,808(-2^63) to 9,223,372,036,854,775,807(2^63 -1)(inclusive). Its minimum value is – 9,223,372,036,854,775,808 and maximum value is 9,223,372,036,854,775,807. Its default value is considered as 0.

The long data type is used when you need a range of values more than those provided by int.

Example: long a = 100L

Float Data Type

It is a single-precision 32-bit IEEE 754 floating-point. Its value range is unlimited.

Its default value is 0.0F.

Example: float a = 1.2f

Double Data Type

It is a double-precision 64-bit IEEE 754 floating-point. Its value range is unlimited.

Its default value is 0.0d.

Example: double d1= 1.2

Char Data Type

It is a single 16-bit Unicode character. Its value-range lies between ‘\u0000’ (or 0) to ‘\uffff’ (or 65,535 inclusive).

The char data type is used to store characters.

Example: char letterA = ‘A’

Leave a Reply