How to declare Variables in Java

Posted by

How to declare Variables in Java

Variable is a container which holds values which are used in Java program. To use this variable which needs to be declared. Just declare variables at the first thing in any program.

This is strongly typed programming language which means that every variable must have data type associated with it. Example, a variable to be declared to use one of the eight primitive data types such as byte, short, int, long, float, double, char or boolean.

A variable can fill to a certain level when we declare a variable to use data type like putting a label on to the bucket which stated that what can be filled with. Lets state for the buck as “Sand”. Once label is said to be attached and can only add or remove sand from bucket. You can try and put anything to it and get stopped by bucket police. In Java, you can now think of compiler as bucket police ensures the programmers to declare and use variables properly.

To declare variable in Java which is needed to the data type which is followed by variable name:

int numberOfDays;

In the above example, variable so called as “numberOfDays” which has been declared with data type of int. Just view the line ends with semi colon. Semi colon tells java compiler that declaration is complete.

Its has been declared, numberOfDays which can only hold value that match definition of data type such as int data type the value can only be a whole number between -2,147,483,648 to 2,147,483,647).

Declaring variables for other data types is exactly the same:

byte nextInStream;

short hour;

long totalNumberOfStars;

float reactionTime;

double itemPrice;


A variable before must be given an intial value. This is so called as intializing variable. If we try to use variable without giving first its value.

int numberOfDays;

//try and add 10 to the value of numberOfDays

numberOfDays = numberOfDays + 10;

the compiler will throw an error:

variable numberOfDays might not have been initialized

Inorder to intialize a variable where we can use assignment statement. An assignment statement is said to follow same pattern as equation in matematics. For eg: 2 + 2 = 4. There is a left side of equation on right side and equal sign (i.e., “=”) onto the middle. To give a perfect variable a value, left side of name of variable and right side is value:

int numberOfDays;

numberOfDays = 7;

At the above example, numberOfDays which was been declared with data type int which was giving an intial value of 7. We now add ten value of numberOfDays as its been intialized.

int numberOfDays;

numberOfDays = 7;

numberOfDays = numberOfDays + 10;


Typically, the initializing of a variable is done at the same time as its declaration:

//declare the variable and give it a value all in one statement

int numberOfDays = 7;


This name given to variable is so called as identifer. The way complier known as variable which deals with the variable’s name.

There are certain rules for specific identifiers:

reserved words cannot be used.

They cannot start with digit but digits to be used after first character so called as name1, n2ame are valid

they must start with a letter, an underscore (i.e., “_”) or a dollar sign (i.e., “$”).

you must use other symbols or spaces (e.g., “%”,”^”,”&”,”#”).

This always give your variables meaningful identifiers. If variable holds price of b ook then call is related as “bookPrice”. If each variable which has name that makes it clear whats been used for, it makes error finding in your program to be lot easier.


Leave a Reply

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