Home >>Java Tutorial >Java Operators

Java Operators

Java Operators

Java Operators are generally used in order to execute the operations on the variables and values. Java language is known to avail the programmers a rich set of the operators to manipulate the variables. Operator in the java language is basically a symbol that is generally used to perform the operations.

Here is an example of the operators in Java, in this example it is being depicted that the + operator are used to add the two values together:

public class Demo 
{
  public static void main(String[] args) 
  {
    int sum = 100 + 50;
    System.out.println(sum);
  }
}

In Java language the + operator is frequently used to add the two values together, just like in the above mentioned example. This operator can be used to add a variable and a value, or a variable and another variable together.

Here is an example for the same:

public class Demo 
{
  public static void main(String[] args) 
  {
    int add1 = 100 + 50;
    int add2 = add1 + 250;
    int add3 = add2 + add2;
    System.out.println(add1);
    System.out.println(add2);
    System.out.println(add3);  
  }
}

There are five types of java operators that are frequently used in the language:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • Bitwise operators

1. Arithmetic Operators

Arithmetic operators are the operators that are generally used in the mathematical expressions in the exact same way that they are used in the algebra. In simple words, arithmetic operators are basically used in order to perform some common mathematical operations.

Here is the table depicted below with the brief description of all the arithmetic operators along with an example:

Operator(Symbol) Name Description Example
+ Addition This operator generally adds two values together x + y
- Subtraction This operator generally subtracts one value from the another x - y
* Multiplication This operator generally multiplies two values x * y
/ Division This operator generally divides one value from another x / y
% Modulus This operator generally returns the division remainder x % y

2. Assignment Operators in Java

Assignment operators are the operators that are generally used to assign the values to variables.1

Here is a table that is depicting the entire assignment operator in the Java language:

Operator(Symbol) Example Same As
= x=10 x = 10
+= x+=10 x=x+10
-= x-=10 x=x-10
*= x*=10 x=x*10
/= x/=10 x=x/10
%= x%=10 x=x%10
&= x&=10 x=x&10
|= x|=10 x=x|10
^= x^=10 x=x^10
>>= x>>=10 x=x>>
<<= x<<=10 x=x<<

3.Comparison Operators in Java

As the name suggests, the comparison operators in Java is used to compare between two values.

Here is the table that is depicting the comparison operator in java along with an example:

Operator(Symbol) Name Example
== This operator is Equal to x == y
!= This operator is Not equal x != y
> This operator is Greater than x > y
< This operator is Less than x < y
>= This operator is Greater than or equal to x >= y
<= This operator is Less than or equal to x <= y

4.Logical Operators in Java

Logical operators are the operators that are basically used to determine the logic between the variables or values. This operator works like its name.

Here is the table that is depicting the logical operators in java along with a description and an example:

Operator(Symbol) Name Description Example
&& Logical and This operator generally returns true if both statements are true x < 5 && x < 10
|| Logical or This operator generally returns true if one of the statements is true x < 5 || x < 4
! Logical not This operator is used to reverse the result, returns false if the result is true !(x < 5 && x < 10)

5.Bitwise Operators in Java

Several bitwise operators are defined by the Java language that can be generally be applied to the integer types, like long, int, char, short, and byte. The working of the bitwise operator generally involves working on bits and they performs bit-by-bit operation.

Here is the table that is depicting the bitwise operators along with the examples and the description:

Operator(Symbol) Description Example
& This is called Binary AND Operator and it generally copies a bit to the result if that exists in both the operands. (A & B) will generally give 12 that is basically 0000 1100
| This is called as Binary OR Operator and it generally copies a bit if to the result it exists in either of the operand. (A | B) will generally give 61 that is basically 0011 1101
^ This is called Binary XOR Operator and it generally copies the bit if this is set in one of the operand but not both. (A ^ B) will generally give 49 that is basically 0011 0001
~ This is called Binary Ones Complement Operator and it is unary and has the effect of 'flipping' the bits. (~A ) will generally give -61 that is basically 1100 0011 in 2's complement form that is due to a signed binary number.
<< This is the Binary Left Shift Operator. The left operands value is moved to the left by the number of the bits that have been specified by the right operand. A << 2 will generally give 240 that is basically 1111 0000
>> This is called as Binary Right Shift Operator. The left operands value is generally moved to the right by the number of bits that are specified by the right operand. A >> 2 will generally give 15 that is basically 1111
>>> This is called Shift right zero fill operator. The left operands value is moved to the right by the number of bits that have been specified to the right operand and has been shifted the values that are filled up with the zeros. A >>>2 will generally give 15 that is basically 0000 1111

No Sidebar ads