Variables in Java: Definition and Usage Explained
Table of Content:
Variables in java
Variable is name of reserved area allocated in memory. In other words, it is a name of memory location. Variables are used to store information to be referenced and manipulated in a computer program. value of the variable can change, depending on conditions or on information passed to the program. variable="vary + able" that means its value can be changed.
It is helpful to think of variables as containers that hold information.
Each variable in Java has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.
You must declare all variables before they can be used. Following is the basic form of a variable declaration
Variable Declaration and Initialization in two step;
data_type variable_name ; variable_name = Constant_value ;
int age ; age = 21 ;
Here,
data_type
is int
variable_name
is age and
Constant_value
21
Variable Declaration and Initialization in one step
data_type variable_name = Constant_value ;
int age = 21 ;
Here,
data_type
is int
variable_name
is age and
Constant_value
21
A variable name can be chosen by the programmer in a meaningful way so as to reflect what it represents in the program. some Examples of variable name;
- average
- height
- classStrength
- roll
- semester
- averageLength
- etc.
Variable may consist of alphabets, digits, underscore(_), and dollar sign;
Rules for naming variables:
- All variable names must begin with a letter of the alphabet, an underscore, or ( _ ), or a dollar sign ($). The convention is to always use a letter of the alphabet. The dollar sign and the underscore are discouraged.
- Digit at start is not allowed.
- A variable’s name can be any legal identifier.
- After the first initial letter, variable names may also contain letters and the digits 0 to 9. No spaces or special characters are allowed.
- The name can be of any length, but don't get carried away. Remember that you will have to type this name.
- Uppercase characters are distinct from lowercase characters. Using ALL uppercase letters are primarily used to identify constant variables. Remember that variable names are case-sensitive.
- You cannot use a java keyword (reserved word) for a variable name.
- White space is not permitted.
Some Standard Conventions Used
1. Never Use Dollar Or Underscore as First Letter
- Dollar Sign and Underscore as First Character in Variable name is allowed but still its not good programming Style to use it as First Character.
- Generally we use Underscore in “Constant Value” variable.
static final int TOTAL_SUM = 10; static final int MAX_HEIGHT = 20;
*Final is nothing but constant value in Java (In C programming we use const
)
2. Capitalization of First Character of Second Word
- If variable name contain two words then write first letter of second word in Capital Case.
- If variable name contain single word then write that word in small case.
Example : Multiple Words in Variable Name
sumOfNumber firstRollno skinColor
Example : Single Word in Variable Name
sum first last