# Basic Operators in Java

By the definition provided by Oracle –

"Operators are special symbols that perform specific operations on one, two, or three operands, and then return a result"

In other words, the operations you do with the variables in your Java Program are represented by some signs. They are called Operators.
For example, Assignment Operator (=). We use it for assigning some value in a variable.

Java provides many other operators. We can divide them according to their categories. We will discuss about all these operators one by one.

## Arithmetic Operators

Simple Arithmetic Operators

 Operator Result Example Meaning + Addition int a = 1+1; int a = 1+1; - Subtraction int b = a-1; int b = a-1; * Multiplication int c = b*2; int c = b*2; / Division int d = c/2; int d = c/2; % Modulus int e = 10%2; int e = 10%2; ++ Increment a++; or ++a; a = a+1; -- Decrement a--; Or --a; a = a-1;

Though most of the operators are common in Arithmetic, Increment and Decrement operators are not.

Increment or decrement operators are used mainly in 2 ways. Either we can use it before the variable (++a) or after the variable (a++).

int f = (++a)+1;

This statement means, first increment the variable by 1(i.e. a=a+1).

Then execute the next statement,

i.e. int f = a+1;

int g = (a++)+1;

This statement means, first int g = a+1;

Then execute the next statement,

i.e.  Make an increment of the variable by 1(i.e. a=a+1).

Similar applies for decrement operator.

--a or a—means, a=a-1;

Sample Program for Simple Arithmetic Expressions

``````public class SimpleArithmeticOperators
{

public static void main(String[] args) {

/**Simple Arithmetic Operators**/

int a = 1+1;
System.out.println("Value of a is : "+ a);

//Subtraction
int b = a-1;
System.out.println("Value of b is : "+ b);

//Multiplication
int c = b*2;
System.out.println("Value of c is : "+ c);

//Division
int d = c/2;;
System.out.println("Value of d is : "+ d);

//Modulus
int e = 10%2;
System.out.println("Value of e is : "+ e);

//Increment Operator
a = 0;
int f = ++a;
System.out.println("Value of f is : "+ f);

a = 0;
System.out.println("Value of a before increment is : "+ a);
int g = a++;
System.out.println("Value of g is : "+ g);
System.out.println("Value of a after increment is : "+ a);

//Decrement Operator
a = 0;
int h = --a;
System.out.println("Value of h is : "+ h);

a = 0;
System.out.println("Value of a before decrement is : "+ a);
int i = a--;
System.out.println("Value of i is : "+ i);
System.out.println("Value of a after decrement is : "+ a);

}

}``````

The output will be - Click "Try It" button above ## Arithmetic Assignment Operators

 Operator Result Example Meaning += Addition Assignment a+=2; a = a+2; -= Subtraction Assignment a-=3; a = a-3; *= Multiplication Assignment a*=4; a = a*4; /= Division Assignment a/=5; a = a/5; %= Modulus Assignment a%=6 a = a%6;

Arithmetic Assignment Operators are quite straight forward and self-explanatory too!

Sample Program for Arithmetic Assignments - Click Try It to compile the below code

``````public class ArithmeticAssignment {

public static void main(String[] args) {
int a = 10;
System.out.println("value of variable before assignment: "+ a);

a+= 2;
System.out.println("value of variable after first assignment: "+ a);

a-=3;
System.out.println("value of variable after second assignment: "+ a);

a*=4;
System.out.println("value of variable after third assignment: "+ a);

a/=5;
System.out.println("value of variable after fourth assignment: "+ a);

a%=6;
System.out.println("value of variable after fifth assignment: "+ a);
}

}``````

The output of this program is – Cick Try It Button above to Compile Scroll to Top