Arithmetic operators

Arithmetic operators are:

2. operator difference in numbers –

3. operator product numbers *

4.  operator quotient of numbers /

5. operator of the remainder (modulo)%

6.  increment and decrement operators:   ++ and —

Operators +, -, *, / and% for the operands is a numerical equivalent of the well-known mathematical operations.

Operators ++ and – –  are unary.    The operator ++ is used to increase one of its operand and the operator – the same decrease the value.

public class operators {
public static void main (String [] args) {
int a = 5;
int b = 4;
int c = a + b;                            // = 9
c = a-b;                                    // = 1
c = a * b;                                 // = 20
c = a / b;                                 // = 1  as the 4 * 1= 4, and is residues 1
c= a % b;                               // 1 = remainder
}
}

A very important role to play in programming operators increment and decrement. They are used to increase or decrease the value of a variable by 1. They are therefore often used in loops.

There are two kinds of them:

prefix – increase the value before assigning

 

inta = 6;
intb = 3;
int c = a + b++;                                // C = a + b, and only then b = b + 1;

c = (a ++) + b;                             // C = a + b, then a = a + 1

 

 

int x = 5;
System.out.println (x ++);
System.out.println (x);
will display twice the number 6,

while the effect of start up code:

int x = 5;

System.out.println (x ++);
System.out.println (x);
will display the number 5 and then the number 6.
Instructions System.out.println (++ x) makes the first is incremented value of the variable x and then this value is read and displayed. Instructions System.out.println (x ++) has the semantics, the first is taken and displayed value of the variable x, and then this value is incremented.

 

 

Close Menu