Basic operations with variables in Java

Facebooktwittergoogle_plustumblrFacebooktwittergoogle_plustumblr

Calculator-Vertex-Academy
Well, in programming it’s impossible to go without addition, subtraction, multiplication, etc. In Java these arithmetic operations are performed by means of so called “operators”. We can divide all Java operators into three groups:

  1. arithmetic operators
  2. logical operators
  3. bitwise operators

In this article we will talk about:

  • arithmetic operators
  • logical operator (in bare outlines)

Logical and bitwise operators will be covered in our separate articles. At this stage it will be enough for you to understand how do arithmetic operators work. Let’s get started!

Arithmetical operators

In fact, many arithmetic operators are already familiar to you from the school:

  • addition
  • subtraction
  • multiplication
  • division

Let’s try to make sense of these operators using following examples:

Example 1

If you will try to run this code on your computer, you will see these numbers in the console:

80
50
250
50

Comments:
Let’s look at each code line.

Here we declared the variable k and assigned the value 70 to it, then we declared the variable p and assigned the value 10 to it, and then we declared another variable f and assigned the value 5 to it.

There we declared the variable m and assigned a value to it, which is equal to the sum of k and p. K equals 70, p equals 10 and, respectively, m equals 80.

Then we displayed the value of m in the console. And, indeed, we see the number 80 in the console.

In this line we specified, that the variable m should get a new value, which equals m - 30. m was equal to 80, so the new value of m will be 80-30, i.e. 50.

The new value is displayed in the console and we see, that it equals 50.

We declare the variable s and assign a value to it, which equals m*f. Since m equals 50 and f is equal to 5, then we multiply 50*5 and get 250. It means, that s equals 250.

We assign a new value to the variable s, which equals s/f. In Java the symbol / stands for division. Now the variable s equals 250, f equals 5. Therefore, by dividing 250 by 5 we get 50. Consequently, s equals 250. I.e. the new value of the variable equals 50.

We display the new value of the variable s in the console. And, indeed, we see the number 50 in the console.

Consider this: In Java the symbol / stands for division

Shortcut arithmetical operators

There is a special type of arithmetical operators, which is called “shortcut arithmetical operator”. You need to know that there are two forms of recording of basic arithmetic operations. For example:

 m += 7;  // is equivalently to m = m+7;
m -= 7 ; // is equivalently to m = m-7;
m*= 7; // is equivalently to m = m*7;
m/= 7; // is equivalently to m = m/7;

Why were shortcut arithmetical operators implemented? Because necessity is the mother of invention! When you write many-many code lines, each additional character takes extra time and effort. It’s much easier to use shortcut arithmetical operators…

To make sure that the shortcut arithmetic operators produce the same results as the full form of arithmetic operations, we suggest you to run these two small programs on your computer. Both Example 2 and Example 3 will display the number 9.

Example 2

If you will try to run this code on your computer, the following number will be displayed in your console:
9

Example 3

If you will try to run this code on your computer, the following number will be displayed in your console:

9

Now let’s look at another arithmetic operator – the remainder operator or modulus operator. We dedicated a separate article to this topic – Remainder operator in Java. And after you will read this article, we will pass on to increment and decrement operators – see below.


Increment and decrement

Well, we have already went through operations in Java, which are very similar to those we know from school. Now it’s time to pass on to the operations which will be a novelty for you, unless you have an experience in programming. Although it’s not a rocket science.  The case is that you often need to perform such operations:

  • to increase the variable by 1
  • to decrease the variable by 1

That’s why they invented separate operations with variables, which are called increment and decrement.

  1. Increment is responsible for increasing a variable by 1. It is designated as ++. For example, if we have a variable I and we apply the increment to it, then it will be written as i++. That means, that the value of the variable i has to be increased by 1.
  2. Decrement is responsible for decreasing a variable by 1. It is designated as --. For example, if we have a variable n and we apply the decrement to it, then it will be written as n --. That means, that the value of the variable n has to be decreased by 1.
Example 4

If you will try to run this code on your computer, then you will see the following number in your console:
3

Comments:
In this line we declared the variable n and assigned a value 2 to it.

Then we used the increment and that’s why the variable n has to be increased by 1. And, respectively, from now on the variable n equals 3.

And we displayed the new value of the variable n by means on this line:


Example 5

If you will try to run this code on your computer, you will see the following number in our console:
1

Comments:
In this line we declared the variable n and assigned the value 2 to it.

Then we used the decrement and therefore the variable n has to be decreased by 1. And, respectively, from now on the variable n equals 1.

And we displayed the new value of the variable n by means on this line:

Two forms of increment and decrement

You need to know that there are two forms of increment:

  1. postfix increment (n++) We have already considered this form of increment in Example 4
  2. prefix increment (++n)

There are two forms of decrement as well:

  1. postfix increment (n--) We have already considered this form of decrement in Example 5
  2. prefix increment (--n)

So what’s the difference between prefix and postfix forms?

The postfix form:

  • first uses the old value in calculations
  • further in subsequent calculations it uses the new value

The prefix form:

  • uses the new value in calculations from the very beginning

It’s better to show it on an example and everything will become clear. Let’s consider the following example:

Example 6

If you will try to run this code on your computer, then you will see the following numbers in your console:
4
3
6
3

Comments:

In these two lines we declared two variables n and k and assigned the same values to them – 2.

In this line we have n++, i.e. the postfix form of increment. That’s why at first it uses the old value for calculations.  Since n = 2, the variable a = 4. And only after this the increment will be applied to the variable and n will equal 3.

In this line we have ++k, i.e. the prefix form of increment. That’s why at first it uses the new value for calculations. Consequently, k equals 3, i.e. 2*3 = 6 and the variable b equals 6.

By means of these 4 lines we displayed the new values of a, n, b and k:
4
3
6
3
We hope that now the difference between prefix and postfix forms is clear.

Also you need to remember, that increment and decrement are applied only to a variable, you cannot apply them to numbers. For this reason, expressions like 8++ or ++8 are considered to be unacceptable.
You should consider another example for postfix and prefix forms of decrement. They work on the same principle as postfix and prefix forms of increment. However, here’s the example:

Example 7

If you will try to run this code on your computer, then you will see the following numbers in your console:
4
1
2
1

Comments:

In these two lines we declared two variables n and k and assigned the same values to them – 2.

In this line we have n--, i.e. the postfix form of decrement. That’s why at first it uses the old value for calculations.  Since n = 2, the variable a = 4. And only after this the decrement will be applied to the variable and n will equal 1.

In this line we have --k, i.e. the prefix form of decrement. That’s why at first it uses the new value for calculations. Consequently, k equals 1, i.e. 2*1 = 2 and the variable b equals 2.

By means of these 4 lines we displayed the new values of a, n, b and k:
4
1
2
1

Logical operators

The following table lists all the logical operators in Java. We will consider using these operators when we reach our article “Conditional operator if. Operator switch”.Comparison-operators-Vertex-Academy

 

LET’S SUMMIRIZE:

All Java operators can be divided into three groups:

  1. arithmetic operators
  2. logical operators
  3. bitwise operators

Arithmetical and shortcut arithmetical operators
m += 7;  // is equivalently to m = m+7;
m -= 7 ; // is equivalently to m = m-7;
m*= 7; // is equivalently to m = m*7;
m/= 7; // is equivalently to m = m/7;
m%=7;// is equivalently to m = m%7;

  • % is the remainder operator. It returns the remainder of division.
  • Increment is responsible for increasing a variable by 1. For example, n++.
  • Decrement is responsible for decreasing a variable by 1. For example, n--.

There are two forms of increment:

  1. postfix increment (n++) We have already considered this form of increment in Example 4
  2. prefix increment (++n) We have already considered this form of increment in Example 6

There are two forms of decrement as well:

  1. postfix increment (n--) We have already considered this form of decrement in Example 5
  2. prefix increment (--n) We have already considered this form of decrement in Example 7

The postfix form:

  • first uses the old value in calculations
  • further in subsequent calculations it uses the new value

The prefix form:

  • uses the new value in calculations from the very beginning

increment and decrement are applied only to a variable, you cannot apply them to numbers. For this reason, expressions like 8++ or ++8 are considered to be unacceptable.

We will cover the bitwise operations in a separate article.

Note: sometimes increment is called incrementing operation, decrement – decrementing operation.

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!

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