Java Variables
A variable is a name given to a memory location. It is the basic unit of storage in a program. The value stored in a variable can be changed during program execution. In Java, all the variables must be declared before use.
1. Declaring variables
- datatype: Type of data that can be stored in this variable.
- variable_name: Name given to the variable.
Syntax:
Example:
Where alphabet and number are then variables of type character and integer.
2. Assigning values to variables
Values can be assigned to a variable directly using assignment operator(=).
Syntax:
Example:
Where A and 100 are the values assigned to the variable alphabet and number.
3. Rules for Naming Variables
- Variable names may consists of alphabets , digits , underscore ( _ ) and dollar sign ( $ ).
- They must not be begin with digits.
- It should not be a keyword.
- White space is not allowed.
- Variable names can't be in length.
- Uppercase and Lowercase are distinct.
Valid and Invalid Variable Names :