Introduction to java arithmetic operators

Java arithmetic operators for beginners

The JAVA arithmetic operators are those that we have learned to deal with in the primary school:

  • +: The plus operator allows to make the addition operation between two numerical values (it allows as well the concatenation of strings: we will return to this later)
  • -:  The minus operator allows the subtraction of one numerical value from the other.
  • *: It allows the multiplication of two numerical values
  • /: It allows making the division operation
  • %: The Modulo operator allows to get the remainder of the Euclidean division of two numerical values

Examples of using JAVA arithmetic operators

int nbr1, nbr2, nbr3;  //Declaration of three variables
nbr1 = 1 + 3;            //nbr1 is equal to 4
nbr2 = 2 * 6;            //nbr2 is equal to 12
nbr3 = nbr2 / nbr1;    //nbr3 is equal to 3
nbr1 = 5 % 2;            //nbr1 is equal to 1, because 5 = 2 * 2 + 1
nbr2 = 99 % 8;           //nbr2 is equal to 3, because 99 = 8 * 12 + 3
nbr3 = 6 % 3;            //nbr3 is equal to 0, because there is no remainder (6 = 3 * 2 + 0)

Remark: Here we learned that we can assign the result of an arithmetic operation to a variable.

The result should be for sure of the same type as the variable.

We also learned the modulo operator. If this operator is clear, you should be able to know how to test if a numeric value is odd or even by using the (% 2) operation.

We will provide other examples of JAVA arithmetic operators to make sure that we have the basic skills:

int nbr1, nbr2, nbr3;    //Declaration of three JAVA variables
nbr1 = nbr2 = nbr3 = 0;  //Initialisation
nbr1 = nbr1 + 1;     //nbr1 = 0, the operation on the right is calculated before assigning
                     //the result to the JAVA variable on the right, so 0 + 1 => nbr1 = 1
nbr1 = nbr1 + 1;     //nbr1 = 1 (see above that its value is changed), so, nbr1 = 1 + 1 = 2
nbr2 = nbr1;         //nbr2 = nbr1 = 2
nbr2 = nbr2 * 2;     //nbr2 = 2 => nbr2 = 2 * 2 = 4
nbr3 = nbr2;         //nbr3 = nbr2 = 4
nbr3 = nbr3 / nbr3;  //nbr3 = 4 / 4 = 1
nbr1 = nbr3;         //nbr1 = nbr3 = 1
nbr1 = nbr1 - 1;     //nbr1 = 1 - 1 = 0

Remark: the value of nbr1 changes during the execution of the program and we notice that we always use the latest value of nbr1 to do our calculations.

There is a syntax (shortcut) that shortens the writing of such operations:

nbr1 = nbr1 + 1;

//or

nbr1 += 1;
//or
nbr1++; // post increment
//or
++nbr1; // pre increment

The four lines of code above do the same thing. It adds one to the variable nbr1.

If the variable nbr1 was equal to 1 before this instruction, it will be equal to 2 after one of these instructions.

Adding a value to a variable is called incrementing this variable.

If we say that we incremented a variable, this means that we added the value 1 to that variable.

But, if we add the value 3 for example to the variable nbr1, we say that we incremented the variable nbr1 by 3.

The instructions nbr1++ and ++nbr1 are used exclusively for adding the value 1 to a variable (we cannot add more than one to the variable nbr1 using these instructions).

However we can use the two first instructions to add any value to the variable nbr1 like what shows the JAVA code below.

nbr1 = nbr1 + 2;
//or
nbr1 += 2;

Here we added the value 2 to the variable nbr1. We say that the variable nbr1 was incremented by 2.

The same thing applies to the subtraction of a certain value from a variable. But this operation is called decrementing a variable.

nbr1 = nbr1 - 1;
//or
nbr1 -= 1;
//or
nbr1--; // post decrement
//or
--nbr1; // pre decrement

Here are some examples for the multiplication and the division JAVA arithmetic operators:

Multiplication by 2:

nbr1 = nbre1 * 2; 
//or
nbr1 *= 2;
Division by 2
nbr1 = nbre1 / 2;
//or
nbr1 /= 2;

The difference between post incrementing and pre incrementing java arithmetic operators

The ++ and – java arithmetic operators applies only on integers. So we cannot use them with float or double data types.

We will take the example of incrementing a variable and the same thing applies to decrementing a variable.

The post incrementing java arithmetic operators

j= i++: the assigning operation occurs before incrementing the i variable: post increment

public class MyFirstClass {
 
public static void main(String[] args) {              
int i = 13;
int j;
j= i++;
System.out.println("post incrementing the variable i");
System.out.println(i);
System.out.println(j);
}
}

The variable I was equal to 13 before the instruction: j= i++;

After the instruction j=i++; the value of i is 14 and the value of j is 13.

The value of the variable j was equal to 13 because of the post increment of the variable i: j variable gets the old value of i variable. Then the i variable gets incremented.

The pre incrementing java arithmetic operators

j=++i: the assigning operation occurs after incrementing the i variable: pre increment

public class MyFirstClass {
public static void main(String[] args) {
int i = 13;
int j;
j= ++i;
System.out.println("pre incrementing the variable i");
System.out.println(i);
System.out.println(j);
}
}

The variable I was equal to 13 before the instruction: j= ++i;

After the instruction j=++i; the value of i is 14 and the value of j is 14.

The value of the variable j was equal to 14 because of the pre increment of the variable i: the i variable gets incremented. Then j variable gets the value of i variable which is the incremented one.

Remark: The pre and post prefixes determine when incrementing occurs with respect to the assignment: if we are in the case of the pre-incrementing operation, the incrementing operation takes place before the assignment and vice versa. If we are incrementing a variable without assigning its value to another variable, the post and pre incrementing operations have the same effect.

The arithmetic operations should be used with variables of the same type. Otherwise, we will get errors or loose precision when doing this with any programming language.

For more about the JAVA assignment operators and when you will lose precision when dealing with java arithmetic operators, please check the correspondent tutorial.

Also to install any necessary JAVA development tool or to know about JAVA variables, please check correspondent tutorials.

This tutorial arrived at its end. So, if you have any questions or remarks about any one of the java arithmetic operators, please don’t hesitate to give us your feedbacks by adding comments and suggestions.

Also, if you want to get the latest JAVA tutorials, please consider following us on Facebook or subscribing our mailing list.







Post a Comment

Previous Post Next Post