Java Variable


Variable is an identifier whose value can be changed.
int x=10;

Varaible declaration rules:

  1. Variable name must be starts with alphabet or underscore or doller symbol.
  2. example:
    int age=10;
    int _age=20;
    int $age=30;
  3. The max length of the variable name is upto 32 characters.
  4. If variable name contains morethan one word ,the second word onwords first letter should be uppercase letter.
  5. example:
    int studentAge=10;
  6. No spaces are allowed at the middle of the variable declaration.
  7. example:
    int student Age=10; (Invalid);
  8. If variable value contains constant value that variable name must be uppercase letter.
  9. example:
    final float PI=3.14;
  10. If constant variable name constains morethan one word ,it should be separated with underscore.
  11. example:
    final float PI_VALUE=3.14;
  12. All keywords are lower case letters.
  13. We should not take keyword as a variable name.
  14. example:
    int if=10;(Invalid) int If=10;(Valid);

Variable Defination types:

  1. Initilization
  2. Declaration
  3. Assignment
  • Initilization:
  • It Means initilize the some value to the variable.
    int x=10;
    Here 10 is initilize to the x.
  • Declaration:
  • In the declaration no value initilize to the variable.
    int x;
  • Assignment:
  • In the assignment assign the value to the variable or assign variable to the variable.
    int x;
         x=10;(It is a value to variable).
    int y;      y=x;(It is a variable to variable).