Variables can be used to store information/data. Once a variable is declared, it can be initialized with some value and later can be changed to some other value. A variable can be used further for performing any calculations, printing it etc., Every variable should have a datatype like
int , boolean etc., and that type should be specified at the time of declaring the variable. Every variable should also have a unique (or non duplicate) name to identify it.
For example, in a student program we can have one variable to store 'the section of the student', one for 'roll number' etc.
char section; char tells the data type of the variable and section gives the variable name. Similarly int is the data type and the variable name is roll_number .
You can assign a value to a variable at the same time that it is declared. This process is known as initialization.
char section = 'C'; int defines the data type of the variable and roll_number gives the variable name, where 76 is the current value of the variable.
As shown above, one variable (roll_number ) in your program can store numeric data while another variable (section ) can store char data. Java has special keywords to signify what type of data each variable stores. We can declare variables using the keywords/types explained in Data Types In Java and initialze the value of the variable.
Rules for naming variables:
Print variable value CODE class PrintVariableValue OUTPUT The class has 34 students and the highest marks are 98. DESCRIPTION Here we declared variables THINGS TO TRY
4-min video about Java Variables
|