Table of Contents
Introduction
Variables are names that are used to identify memory locations that store values. Each variable belongs to a particular data type. Data types determine the size of the memory location.In this article, we will be exploring Java variables and data types in detail.
1. Variables
A variable can be used to refer to a value at a memory location. The code can use and modify the value at the memory location using the variable. A variable needs to be declared and initialized before it can be used.
In order to declare a variable, you need to specify its data type and a name for the variable as shown below:
int i; //declares a variable called i of int data type
Here, int is the data type. It is used to store a non-negative positive or negative whole number. Just like int, there are several other data types supported by Java which are covered in the next section.i is the variable name. It points to the memory location which the operating system has allocated.
You can also declare several variables at once as follows:
int i,j,k; //declares 3 variables called i,j,k of int data type
Note that the variable names are separated using commas.
When a value is assigned to a variable, the variable is said to be initialized. There are several ways you can initialize a variable:
You can initialize a variable when it is declared as follows:
int i = 100; // creates a variable i and sets it’s value to 100
You can declare a variable first and initialize it later on as follows:
int i; // declares int variable i //do something else i = 100; // sets i to 100
You can also use an expression to initialize as variable as follows:
int sum = 10+6; //sets sum to 16 int sum2 = a+b; // adds the values in a and b and assigns the result to sum2
Once you declare and initialize a variable, you can use it in your code as follows:
int a = b+27; // uses b in an expression System.out.println(“a = “+a); // prints value of a if(a > 20) { // uses a in an if statement // do something }
2. Data Types
Data types determine the type of data that a variable can contain and its size. The operating system allocates memory to a variable based its data type. Java supports several data types which can be used to store different type of data. Data types can be categorized as follows:
1) Primitive Data Types
Primitive data types are types used to store a single value. They are basically used to store numbers and characters. Primitive data types can be further classified into the following groups:
(a) Integer Data Types
Integer data types can be used to store whole numbers, that is non-fractional positive or negative numbers. Java supports byte, short, int and long integer data types as detailed below:
Name | Width (Bits) | Range | Example |
byte | 8 | –128 to 127 | byte b = 100; // creates variable b of byte data type with value 100
byte b1 = 128; //will cause error as maximum value is 127 |
short | 16 | –32,768 to 32,767 | short s = 1000; // creates variable s of short data type with value 1000 |
int | 32 | –2,147,483,648 to 2,147,483,647 | int i = -4507; // creates variable i of int data type with value -4507 |
long | 64 | –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | long l = 125000000; // create variable l of long data type with value 125000000 |
As can be seen from the table above, the difference between them is the range of values that they can store. While byte can be used to store very small numbers, long can be used to store bigger numbers.
(b) Decimal Data Types
Decimal data types can be used to store decimal numbers that is numbers having a fractional component. Java supports the float and double data types to store decimal numbers as detailed below:
Name | Width (Bits) | Range | Example |
float | 32 | 1.4e–045 to 3.4e+038 | float f = 2.5; // compilation error
float f1 = 3.5f; //creates a float variable f1 with value as 3.5 |
double | 64 | 4.9e–324 to 1.8e+308 | double d = 100.25; // creates a double variable d with value as 100.25 |
The difference between the two is the precision level. Float specifies a single precision value whereas double specifies a double precision value. Single precision takes less space as compared to double precision but are not as accurate as double precision, particularly for very large or very small numbers.By default, when you create a decimal number, Java expects it to be of double data type, so if you want to explicitly create a float value, you need to append an ‘f’ at the end as done above.
(c) Characters
Java supports the char data type to store characters. This can be used to store characters like letters, numbers and symbols as shown below:
Name | Width (Bits) | Range | Example |
char | 16 | 0 to 65536 | char c = ‘a’; // creates a char variable with value ‘a’ |
Java uses Unicode to store characters. Unicode is an international character set that can store characters from many languages. So in addition to the English language character like ‘a’, b’, etc, the char data type can be used to store characters from other languages too.
(d) Boolean
Java supports the boolean data type to store ‘true’ or ‘false’ as demonstrated below:
Name | Width (Bits) | Range | Example |
boolean | 1 | true/false | boolean b = true; // assigns the value true to b |
The boolean data type is basically used for comparisons or to test conditions.
3. Reference Data Types
In addition to primitive data types, Java supports an additional data type called reference data type. These are used to hold more complex data created using the primitive data types in the form of objects. This will be covered later on.
Conclusion
So this article covers declaring, initializing and using Java variables. It also walks you through the various data types supported by Java.