Variables And Data Types in Java

Introduction 

 
We frequently use variables and data types in our programs. It is even difficult to imagine an application without these two fundamental concepts.
Now, the question is "What are variables & data types and why do we use them in our programs?".
 
Variables are generally considered to be a memory to keep data. However, suppose you are writing a program which uses some data, then you must need some memory to store this data. So variables are used to reserve memory for storing data and those reserved memory locations are identified by the name of the variable.
 
Note: If you are familiar with the 'C' or 'C++' Language, you may know how to get the memory address of a variable, but you don't do that in Java.
 
I hope it is now clear "what is the use of variables". Now, what about "how much memory should be kept or reserved".
 
Variables can't be used directly to reserve memory. One must also specify the Data Type with the variable name to ensure the amount of memory to be kept to store the data. The variable is restricted to hold only a specified type of data; when you assign another type of data to a variable then the output may be undesirable or garbage.
 
Syntax to declare a variable
 
[data type] [variable name];
 
For e.g. 
  1. package csharpcorner;  
  2.   
  3. public class NewClass 
  4. {  
  5.     int i;  
  6.           
  7. }  
The above line of code declares the variable i of integer type.
 
Syntax to initialize a variable
 
[data type] [variable name] = data; //Declaration and Initialization

OR

 
[data type] [variable name];  //Declaration
[variable name] = data; //Initialization
 
For e.g.
  1. package csharpcorner;    
  2.     
  3. public class NewClass   
  4. {    
  5.     int i = 10;           
  6. }   
OR
  1. package csharpcorner;    
  2.     
  3. public class NewClass   
  4. {    
  5.     int i;    
  6.     i=10;     
  7. }    

Naming variables

 
One should consider these things while naming a variable:
  • A variable name must start with an alphabetic ('a to z' or 'A to Z') character or one of the "_" or "$" special characters only.
  • A variable name must not contain any special character except "_" and "$".
  • A variable name must not start with a digit.
  • Digits can be used in variable names.
  • A variable name must not be a predefined keyword. For e.g., main, class, float, int, etc.
Examples of some allowed variable names:
  • abc
  • ABC
  • AbC
  • a11111
  • $fsdf
  • _flkd
  • abr_$dff
Example of some illegal variable names:
  • 1abc
  • @dff
  • -number
  • int
  • class
  • fdgfd-fkl
  • abc~

Data Types

 
Java defines eight predefined (primitive) types of data which are categorized in the following four categories:

1. Integer Type

 
Name Width Range
byte  8 -128 to 127 (-27 to 27 -1)
short 16 -32,768 to 32,767 (-215 to 215 -1)
int 32 -2,147,483,648 to 2,147,483,647 (-231 to 231 -1)
long 64 -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (-263 to 263 -1)

2. Floating-Point Type

 
Name Width Range
float 32 3.4e-045 to 3.4e+038
double 64 4.9e-324 to 1.8e+308

3. Character Type

 
Name Width Range
char 16 0 to 65536

Note: Java uses UNICODE which defines a fully international character set that can represent all of the characters found in all human languages.

4. Boolean Type

 
The "boolean" type accepts only one of two possible values i.e. "true" or "false".