Simple explanation of Java Operators

Simple explanation of Java Operators

Operators might be helpful for you to know ahead of time which operators have the highest precedence when we investigate the Java operators. Using variables and values operations are performed in Java with Operators. It might be helpful for you to know ahead of time which operators have the highest precedence when we study the Java programming language’s operators.

An operator’s precedence increases with distance from the top of the table. Higher precedence operators are examined before comparatively lesser precedence operators. Operators on the same line are prioritized equally. A rule must specify which operator is evaluated first when two operators with the same precedence appear in the same expression. Except for the assignment operators, which are assessed right to left, all binary operators are evaluated from left to right.

What are Operators?

The necessary operation or task is defined by the Java Operators. These operations may range from logical to mathematical. The things that operators perform functions on are referred to as “operands”.  An operator’s precedence increases with distance from the top of the table. I will make a table of all the necessary operators used in Java. Using variables and values operations are performed in Java with Operators.

Below is the example “+” is used to add to values together:

int x = 100 + 50;

Despite the fact that the + operator is frequently used to combine two values, as in the example above, it can also be used to combine a variable with a value or a variable with another variable:

int num1 = 1000 + 500;        // 1500 (1000 + 500)
int num2 = num1 + 2500;      // 4000 (1500 + 2500)
int num3 = num2 + num2;     // 8000 (4000 + 4000)

Also read, Why Java is so popular among Developers!

Different Types of Operators

Java has specified various types of operators to perform operations.

It has been divided into 5 different operators: arithmetic, assignment, comparison, logical operators, and bitwise operator.

Arithmetic Operators

Common mathematical problems are used to solve using arithmetic operators that include addition, subtraction, multiplication, division, modulus, increment, and decrement. I will be demonstrating all of these in the table below:

Operator Name Description Example
+ Addition Two values are added together. x + y
Subtraction One value is subtracted from another. x – y
* Multiplication Two values are multiplied. x * y
/ Division One value is divided by another. x / y
% Modulus It returns the remainder after being divided x % y
++ Increment The increment is used to increase the value of the variable by 1. + + x
– – Decrement Decrement is used to decrease the value of the variable by 1. – – y

Also read, Creative human prediction vs. the benefits of AI: in conversation with AlpacaJapan

Assignment Operators Java

With the help of assignment operators in Java, we can assign values to variables. Generally, we use ( = ) assignment operators to assign value. In the example below we use the ( = ) assignment operator to assign 20 to a variable named x.

int x = 10;

The addition assignment variable ( += ) adds value to the variable.

public class Main
{
  public static void main (String[]args)
  {
    int x = 10;
      x += 5;
      System.out.println (x); //15

  }
}

The printed value will be 15.

Table of assignment operators

Operator Example Same as
= x = 7 x = 7
+= x + = 7 x = x + 7
-= x – = 7 x = x – 7
*= x * = 7 x = x * 7
/= x / = 7 x = x / 7
%= x % = 7 x = x % 7
&= x & = 7 x = x & 7
|= x | = 7 x = x | 7
^= x ^ = 7 x = x ^ 7
>>= x>>=7 x = x >> 7
<<= x<<=7 x = x << 7

Also read, Writing Your First Program in Java

Java Comparison Operators

This operator is used to compare values and is called a comparison operator.

Operator Name  Example
== Equal To x == >
!= Not Equal To x != >
> Greater Than x > y
< Less Than x < y
>=   Greater Than or Equal To x >= y
<= Less Than or Equal To x <= y

Logical Java Operators

To determine values between variables and values we use Logical Operators.

Operator Name Description Example
&& Logical And  If both statements are true it returns true x < 5 && x < 10 
|| Logical Or If one of the statements is true it returns true. x < 5 || x < 4
! Logical Not It reverses the result and returns false if the result is true. !( x < 5 && x < 10)

Bitwise Operators

Bitwise operators are tools for performing operations on a number’s individual bits. They are applicable to all integral types (char, short, int, etc.). They are employed in the updating and searching of binary indexed trees.

Name Operator Example
Bitwise AND & opt1 & opt2
Bitwise exclusive OR ^ opt1 ^ opt2
Bitwise inclusive OR | opt1 | opt2
Bitwise Compliment ~ ~ opt
Bitwise left shift << opt1 << opt2
Bitwise right shift >> opt1 >> opt2
Unsigned Right Shift Operator >>> opt >>> number of places to shift

Also read, Learn about Java Datatypes and how we use them

Final Words

Various Java operators can be used, depending on the circumstance. They are organized by the features they provide. When we study the operators of the Java programming language, it might be useful for you to know in advance which operators have the highest priority.

In Java, operations are carried out by using variables and values, and operators. The arithmetic operator, which includes addition, subtraction, multiplication, division, modulus, increment, and decrement, is used to solve typical mathematical problems. We can assign values to a specified variable by using the Assignment operator.

The comparison operator is the name of the operation that is used to compare values. Logical operators are employed to establish correlations between variables, and values. The Bitwise operators are tools for carrying out operations on each bit of a number. 

I hope you are getting through the blog if you have any doubt regarding the blog please let me know in the comment or you can contact me.