Datatypes and Variables in Java
Datatype is the type of data that is to be stored in a program. It says the compiler how much memory need to be allocated for storing values based on type. It is used to reserve a memory in RAM for storing values.
datatype variable_name;
Without declaring a variable i.e specifying a datatype, a variable cannot store value.
Variables in Java
Variable is a name of the reserved storage address which holds a value. To store a value we should first declare a variable.
Below is the way of declaration of variable.
datatype variable_name;
To assign a value to the variables we can use like this below. This is said as initialization.
variable_name = value;
Both initialization and declaration can also be done in one line like below.
datatype variable_name = value;
Example
public class VariableTest {
public static void main(String args[]) {
int a_value=5;
}
}
}
Try In EditorWe should follow the rules and conventions for naming a variables which will be discussed in next topic.
What is a variable or datatype and why they are used detailly ?