For instance, the Java operators that perform addition, subtraction, division, multiplication, etc. are placed in the arithmetic category of operators. Similarly, the operators that provide the relationship between variables are placed inside the relational operator’s category.

This article lists down the usage and applicability of the basic operators in Java. So, let’s start.

## What are the basic operators in Java

This section describes the functionality of basic operators in Java.

## Assignment Operators

These operators assist in assigning value to a variable in Java. This assignment action can be performed using one of the following operators:

**“=”: **works on two operands and assigns the value placed on the right side to the variable written on the left side.

**“+=”:** adds operands of both sides and then assigns the value to the variable written on the left side

**“-=”: **subtracts operand placed on right from left, and then assigns the value to the variable placed on left side

**“*=”: **multiplies both operands and then assigns the answer to the variable on the left side

**“/=”: **reports the remainder by dividing the operands or dividing the left operand by a specific value.

**“%=”: **gets the remainder using this % first, and then the answer is assigned to the variable on the left side.

The following syntaxes may be followed:

operand1+=operand2 //using +=

operand1-=operand2 //using -=

operand1*=operand2 //using *=

operand1/=operand2 //using /=

operand1%=operand2 //using %=

## Arithmetic Operators

This category deals with the arithmetic operations and the following operators reside in this category.

**“+”: **used to add two variables/values

**“-“: **provides the difference between two variables/value

**“*”: **multiplies two variables/values

**“/”: **used to divide one variable over another and the quotient is shown in the output.

**“%”: **reports the remainder() of two variables/values

The syntax of these operators is provided below, the operand1 and operand2 refer to the variables/values. It is observed that two operands are required to carry out arithmetic operations.

operand1-operand2; //subtraction

operand1*operand2; //multiplication

operand1/operand2; //division

operand1%operand2; //remainder

## Unary operators

This type performs various operations on one variable/value. The operators included in this category are described below:

**“+”**: assigns positive sign to an operand(usually positive sign is not displayed as it is optional)

**“-“**: changes the sign of an operand

**“++”**: increments the value of an operand by 1. The increment operator can be applied as postfix and prefix

**“–“**: the value of a variable/value is decremented by 1. Like increment, it can also be used as a prefix or postfix

**“!”**: the Boolean value(true/false) is inverted by using this operator

You may refer to the syntax of these operators provided below:

-operand; //unary minus

++operand; //prefix increment

operand++; //postfix increment

operand--; //postfix decrement

--operand; //prefix decrement

!operand; //logic complement

## Logical Operators

These operators assist in performing logical operations such as AND, OR, and NOT. These are described below:

**AND(&&): **this operates on two variables/values, it returns true if both variables are true and false in other cases.

**OR (||): **this operator builds logic such that, if both values are false then the result would be false otherwise true.

**NOT (!): **this also refers to the unary category and returns false/true results

You may refer to the following syntaxes to use logical operators:

operand1 || operand2 //logical OR

!operand //logical NOT

## Bitwise Operators

This operator’s class deals with the binary values and thus all the operations are performed bit by bit. and the following operators are practiced:

**“&”: **This is known as binary AND that works the same as logical AND but on binary values.

**“|”: **this also works on the same pattern as logical OR, but it performs the operation bit by bit.

**“^”: **this is known as XOR, it returns true if both values are different and returns false if both values are the same.

**“~”: **this operator changes the bit from 0 to 1 and 1 to 0

**“<<“: **this left shift operator shifts the number of bits to the left. The number of bits is decided by the user and it can be any number.

**“>>”: **the right shift operator shifts removes the number of bits from the right side.

**“>>>”: **this is known as unsinged right shift and it shifts the number of bits by “0”

The following syntaxes refer to the bitwise operators:

operand1 & operand2; //bitwise AND

operand1 ^ operand2; //bitwise XOR

~operand; //bitwise complement

operand<<number; //bitwise left shift

operand>>number; //bitwise right shift

operand>>>number; //bitwise unsigned right shift

## Relational Operators

This category refers to relating more than one variable using specific symbols. These operators assist in making decisions in Java programming.

**“==”: **this operator is practiced checking the equality of two operands

**“!=”: **used to check that the inequality of the operands

**“<“: **practiced checking less than relation between two operands

**“>”: **it is used to check the left operand is higher or not

**“>=”: **to check that the left operand is “greater than or equal” to the right or not

**“<=”: **it is practiced checking whether the left variable is less than or equal to the right

The relational operators can be practiced by using the syntaxes provided below:

operand1!=operand2; //not equal to

operand1>operand2; //greater than

operand1<operand2; //less than

operand1>=operand2; //greater than or equal to

operand1<=operand2; //less than or equal to

## How to use basic operators in Java

This section provides the usage of basic operators in Java. Each example demonstrates the Java code that uses operators of a specific category.

## Example 1: Using the Assignment operators

The following Java code practices various assignment operators on variables.

public class AssignmentOps {

public static void main(String[]args) {

//using "=" to assigning values

int a=3, b=4, c=5, d=6, e=7;

//using "+=" on a

a+=3;

System.out.println("New-Value of a would be: " +a);

//using "-=" on b

b-=3;

System.out.println("New-Value of b would be: " +b);

//using "*=" on c

c*=2;

System.out.println("New-Value of c would be: " +c);

//using "/=" on d

d/=2;

System.out.println("New-Value of d would be: " +d);

//using "%=" on e

e%=2;

System.out.println("New-Value of e would be: " +e);

}

}

The above-stated code is described here:

- firstly, the values are assigned to variables using =
- the “a+=3” statement adds 3 to the current value of a
- subtracting 3 from b using “-=”
- multiplies the value of c by 2 using “*=”
- divides the value of d by 2 using “/=”
- the value of e is divided by 2 and then remainder is stored as a new value of e.

The output of the code is provided below:

## Example 2: Using Arithmetic operators

The following Java code performs arithmetic operations on two numbers a=5 and b=11.

public class ArithOp {

public static void main(String[]args) {

//initializing variables

int a=5, b=11;

//using + on a and b

System.out.println("a+b=" + (a+b));

//using - on a and b

System.out.println("a-b=" + (a-b));

//using * on a and b

System.out.println("a-b=" + (a*b));

//using / on a and b

System.out.println("a/b=" + (b/a));

//using % on a and b

System.out.println("a%b=" + (a%b));

}

}

In the above-stated code, the variables are initialized first and then each line represents the application of a different assignment operator.

The output is provided below:

## Example 3: Using Relational Operators

The following Java code practices the relational operators on two variables x=3 and y=5.

public class RelOp {

public static void main(String[]args) {

//initializing variables

int x=3, y=5;

//using < operator

System.out.println("is x less than y? " + (x operator

System.out.println("is x greater than y? " + (x>y));

//using == operator

System.out.println("is x equal to y? " + (x==y));

}

}

The above-written code implements three relational operators on x and y. Furtherly, the code is described as:

- initializes two variables x and y
- reports the result of x<y
- prints true or false results for condition x>y
- checks and prints the equality of x and y

The output of the code is provided below:

## Example 4: Using logical Operators

The following Java code practices the logical operators in if-else conditional statement.

The above code is described as.

- three variables are initialized
- in the if condition, we used the AND(&&) operator between b==c and c!=a. Moreover, this statement is joined with a==b using OR(||) operator.
- the above-stated condition is true therefore the if-block of if-else statement would be executed.

The output of the code is shown in the image below:

## Example 5: Using unary operators

The unary operators are practiced in the following Java code.

The description of the code is:

- two variables a and b are initialized
- prints the answer after applying unary minus on a
- shows the result after applying prefix increment operator on b

The output of the code is provided here:

## Example 6: Using bitwise operators

The following Java code implements several bitwise operators on variables/values.

The code is described as:

- a and b variables are initialized
- used the left-shift operator on a and the answer is printed. The binary of number 3 would be left-shifted by 2bits.
- prints the answer after applying the right shift operator on b. The 3bits from number b would be removed from the right side of the binary of number 3.

The output of the code is:

## Conclusion

The basic operators in Java are the operators that are used frequently in Java programs. Java supports a long list of operators that assist in performing various operations on variables/values. The operators having alike applicability are placed in the same category. For instance, the operators that define the relationships can be found in the relational category of operators. This article lists down the basic operators in Java and provides their application using Java code. You would have learned the overview and preliminary applicability of all the basic operators in Java.