How to assign a value to a variable in Java

Facebooktwittergoogle_plustumblrFacebooktwittergoogle_plustumblr

Equals Vertex Academy

Assigning of a value to a variable is carried out by means of = symbol.  Consider the following examples:

Example No.1

Let’s say we want to assign a value to a variable x of 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, first of all we declared the variable k of int type in this line:

int k;

Then in another line we assigned the value 10 to the variable k:

k = 10;

As you have already understood from the example, the = symbol is an assignment operation. It always works from right to left:

Variables Announement Vertex Academy

The value 10 is assigned 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 k of the int type and assigned the value to it in one line:

int k = 10;

So now you know, that:

  1. The = symbol is responsible for the 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 or two lines.
What is a variable initialization?

In fact, you already know what it is. Initialization is an 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 hear:

  • “It is necessary to initialize the variable” means that “it is necessary to assign the initial value to the variable”
  • “The variable is initialized” means “we have assigned the initial value to the variable”

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 the variable number1 of the byte type and with the help of = symbol assigned the value 15 to it

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

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

In this line we declared the variable title of the string type and with the help of = symbol assigned the value “I love Java” to it. Since the variable falls into the string data 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 = symbol assigned the value “M” to it. Note that since the variable falls into the char data 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 = 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 = 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 need to remember: it is necessary to add f to the values of the float variables. Though the number 3.14 will be without f in the console.

In this line we declared the variable result of the boolean type and with the help of = symbol assigned the value true to it.

Then we display the values of all variables in the console with the help of

 


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

assign value to a variable Vertex Academy

The value 10 is assigned 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 put it in double quotes:

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

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

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

 

 

 

Facebooktwittergoogle_plustumblrFacebooktwittergoogle_plustumblr

Facebooktwittergoogle_plustumblrFacebooktwittergoogle_plustumblr

Publiction author

offline 4 hours

Vertex Academy

Comments: 0Publics: 274Registration: 06-03-2016

Leave a Reply

Your email address will not be published.

Subscribe news

Subscribe to our newsletter we will keep you up-to-date!

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