How to Assign a Value to a Variable in Java

Facebooktwittergoogle_plustumblrFacebooktwittergoogle_plustumblr

Equals Vertex Academy

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

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:

Variables Announement Vertex Academy

This assigns the value "10" to the variable "k."

Way No.2

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:

  1. The "=" symbol is responsible for assignment operation and we assign values to variables with the help of this symbol.
  2. 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

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:

In this line, we declared variable number1 of the byte type and, with the help of the "=" symbol, assigned the value 15 to it.

In this line, we declared variable number2 of the short type and, with the help of the "=" symbol, assigned the value 100 to it.

In this line, we declared variable number3 of the long type and, with the help of the "=" symbol, assigned the value 100000000 to it.

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.

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.

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.

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").

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

 


LET’S SUMMARIZE:
  1. We assign a value to a variable with the help of the assignment operator "=." It always works from right to left.

assign value to a variable Vertex Academy

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
  1. 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:

If we assign a value to a variable of the char type, we need to put it in single quotes:

If we assign a value to a variable of the float type, we need to add the letter "f":

  1. "Initialization" means “to assign an initial value to a variable.”

 

 

 

Facebooktwittergoogle_plustumblrFacebooktwittergoogle_plustumblr

Facebooktwittergoogle_plustumblrFacebooktwittergoogle_plustumblr

Leave a Reply

Your email address will not be published.

Самоучители--узнать детальнее--