Published May 6, 2021Updated Aug 22, 2022
Contribute to Docs
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.
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Java on Codecademy
Computer ScienceLooking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
Includes 6 Courses
With Professional Certification