JAVA Variables
- Variables are container which is used to store some data values.
- The types and size of value will be declared by the data types.
- We use assignment operator (=) to assign a value to a variable.
Rules Must Be Followed:
⮚ The first character must always be an alphabet or an underscore.
⮚ It should be formed using only letters, numbers, or underscore.
⮚ Uppercase and lowercase are distinct
⮚ A keyword cannot be used as a variable name.
⮚ It should not contain any whitespace character.
⮚ The name must be meaningful.
Declaring The Variable
As a programmer we can create a variable in two approaches, you must specify the data type and assign it a value:
Approach 1:
datatype variable_name ; //variable declarationvariable_name=value; //variable intialization
Declaring The Variable
As a programmer we can create a variable in two approaches, you must specify the data type and assign it a value:
Approach 1:
datatype variable_name ; //variable declaration
variable_name=value; //variable intialization
Syntax:
int a;a=100;
int a;
a=100;
Approach 1:
datatype variable_name=value; //variable declaration and inialization
Syntax:
int a=100;double b=100.11;
int a=100;
double b=100.11;
Program:
public class variable{
public static void main(String args[])
{
int a = 10;
System.out.println(a);
}
}
Output:
10