How to Assign a Value to a Variable in Java
The assigning of a value to a variable is carried out with the help of the "=" symbol. Consider the following examples:
Example No.1
Let’s say we want to assign the value of "10" to the variable "k" of the "int" type. It’s very easy and can be done in two ways:
Way No.1
1 2 3 4 5 6 7 |
class Test { public static void main(String args[]){ int k; k = 10; System.out.println (k); } } |
If you try to run this code on your computer, you will see the following:
10
Comments:
In this example, we first declared the variable to be "k" of the "int" type:
int k;
Then in another line we assigned the value "10" to the variable "k":
k = 10;
As you you may have understood from the example, the "=" symbol is an assignment operation. It always works from right to left:
This assigns the value "10" to the variable "k."
Way No.2
1 2 3 4 5 6 |
class Test { public static void main(String args[]){ int k = 10; System.out.println (k); } } |
If you try to run this code on your computer, you will see the following:
10
Comments:
As you can see, in this example we declared the variable as "k" of the int type and assigned the value to it in a single line:
int k = 10;
So, now you know that:
- The "=" symbol is responsible for assignment operation and we assign values to variables with the help of this symbol.
- There are two ways to assign a value to variables: in one line or in two lines.
What is variable initialization?
Actually, you already know what it is. Initialization is the assignment of an initial value to a variable. In other words, if you just created a variable and didn’t assign any value to it, then this variable is uninitialized. So, if you ever hear:
- “We need to initialize the variable,” it simply means that “we need to assign the initial value to the variable.”
- “The variable has been initialized,” it simply means that “we have assigned the initial value to the variable.”
Here's another example of variable initialization:
Example No.2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
class Test { public static void main(String args[]){ byte number1 = 15; short number2 = 100; long number3 = 100000000; String title = "I love Java"; char letter = 'M'; double sum = 145.34567; float pi = 3.14f; boolean result = true; System.out.println(number1); System.out.println(number2); System.out.println(number3); System.out.println (title); System.out.println (letter); System.out.println (sum); System.out.println (pi); System.out.println (result); } } |
If you try to run this code on your computer, you will see the following:
15
100
100000000
I love Java
M
145.34567
3.14
true
Comments:
1 |
byte number1 = 15; |
In this line, we declared variable number1 of the byte type and, with the help of the "=" symbol, assigned the value 15 to it.
1 |
short number2 = 100; |
In this line, we declared variable number2 of the short type and, with the help of the "=" symbol, assigned the value 100 to it.
1 |
long number3 = 100000000; |
In this line, we declared variable number3 of the long type and, with the help of the "=" symbol, assigned the value 100000000 to it.
1 |
String title = "I love Java"; |
In this line, we declared the variable title of the string type and, with the help of the "=" symbol, assigned the value “I love Java” to it. Since this variable belongs to the string type, we wrote the value of the variable in double quotes.
1 |
char letter = 'M'; |
In this line, we declared the variable letter of the char type and, with the help of the "=" symbol, assigned the value “M” to it. Note that since the variable belongs to the char type, we wrote the value of the variable in single quotes.
1 |
double sum = 145.34567; |
In this line, we declared the variable sum of the double type and, with the help of the "=" symbol, assigned the value "145.34567" to it.
1 |
float pi = 3.14f; |
In this line, we declared the variable pi of the float type and, with the help of the "=" symbol, assigned the value “3.14f” to it. Note that we added f to the number 3.14. This is a small detail that you'll need to remember: it's necessary to add f to the values of float variables. However, when you see it in the console, it will show up as just 3.14 (without the "f").
1 |
boolean result = true; |
In this line, we declared the variable result of the boolean type and, with the help of the "=" symbol, assigned the value "true" to it.
Then we display the values of all the variables in the console with the help of
1 2 3 4 5 6 7 8 |
System.out.println(number1); System.out.println(number2); System.out.println(number3); System.out.println (title); System.out.println (letter); System.out.println (sum); System.out.println (pi); System.out.println (result); |
LET’S SUMMARIZE:
- We assign a value to a variable with the help of the assignment operator "=." It always works from right to left.
This assigns the value "10" to the variable "k."
2. There are two ways to assign a value to a variable:
- in two lines
- or in one line
- You also need to remember:
If we assign a value to a variable of the string type, we need to put it in double quotes:
1 |
String title = "I love Java"; |
If we assign a value to a variable of the char type, we need to put it in single quotes:
1 |
char letter = 'M'; |
If we assign a value to a variable of the float type, we need to add the letter "f":
- "Initialization" means “to assign an initial value to a variable.”