Variables are used whenever there’s a need to store a piece of data. A variable contains data that can be used in the program elsewhere. Using variables also ensures code re-usability since it can be used to replace the same value in multiple places.
Declaring a Variable
To declare a variable in Java, any of these three keywords can be used along with a variable name:
- The data type.
- The variable name.
- The value.
int age = 28;char grade = 'A';boolean late = true;byte b = 20;long num1 = 1234567;short no = 10;float k = (float)12.5;double pi = 3.14;
The equal sign
= is used to assign a value to a variable. After the initial assignment is made, the value of a variable can be updated to new values as needed.
- christian.dinh2481 total contributions
- Anonymous contributorAnonymous contributor3077 total contributions