Variables in Java


A variable is the name of a memory location which is used to store data values. It is like a container for storing data value. It is the way to represent memory location by user-defined name so that it can be easily accessible. Its value can be changed and reused.

Syntax for declaring variables

type variable_list;

Example of declaring variables

int var1;
float var2;
char var3;

In above example var1, var2, var3 are variables and int, float, char are data types.

We can also provide values to variables while declaring it. Below examples shows how to provide values to variables while declaring.

int var1=5;
float var2=5.2;
char var3='c';

Rules for declaring variables

  • Variable names must start with alphabets or an underscore ( _ )
  • Variables name must not contain space in between.
  • You cannot use the reserved keyword of java as a variable.

Invalid variable names

int 5;
int a b;
int char;

Leave a Comment

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