24 Jun Java Variables
In this lesson, we will learn how to work with Java Variables. Variable in Java is a reserved area allocated in memory. It is a container that holds value. Each variable is assigned a type, which is known as data type.
The reserved area set for the variable stRank is shown below. Here, the variable is assigned a value 5. Also, you can see, we need a datatype to declare a variable. The int data type is used below.
Declare Variables in Java
As in the above figure, variables are declared by adding a data type, with the variable name.
Here are some examples:
1 2 3 4 5 6 |
int stRank: int score; byte a; boolean result; |
Initialize Variables in Java
To initialize a variable, assign a value to it. Here are some examples:
1 2 3 4 5 6 |
int stRank = 5; int score = 90; byte a = 10; boolean result = true; |
Here’s the usage of variables in a Java program:
For example,
1 2 3 4 5 6 7 8 9 |
public class Studyopedia { public static void main(String[] args) { int score = 90; } } |
The value of the integer variable above is assigned 90.
No Comments