Java Operators Tutorial – Types of Operators in Java

1. Java Operators – Objective

In our last tutorial, we discussed Variables in java. Now, in this Java operators tutorial, we are going to learn about the various operators used in Java language, their classification and the use of Java operators with examples.

So, let’s start Operators in Java.

Java Operators- Types of operators in java

Java Operators- Types of operators in java

2. Define – Java Operators

Java is a very popular language. There are many java features and operators in java which can be used for different java operations. Here, Java operators list describe operators in java. Before we see the operators, let’s revise Java Syntax

  1. Arithmetic Operators
  2. Unary Operators
  3. Assignment Operators
  4. Relational Operators
  5. Logical Operators
  6. Ternary Operators
  7. Bitwise Operators
  8. Shift Operators

3.  Types of Operators in Java

Let’s discuss Java operators in detail:

a. Arithmetic Operators in Java

Java Arithmetic operators are used for simple math operations, they are

  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)
  • Modulo (%)

Before we start with the example in Java, let us learn about java classes and objects.

Example-

public class operators
  {
     public static void main(String[] args)
         {
             int a = 20, b = 10, c = 0, d = 20, e = 40, f = 30;
             String x = "Thank", y = "You";
// + and - operator
             System.out.println("a + b = "+(a + b));
             System.out.println("a - b = "+(a - b));
// + operator if used with strings
// concatenates the given strings.
             System.out.println("x + y = "+x + y);
// * and / operator
             System.out.println("a * b = "+(a * b));
             System.out.println("a / b = "+(a / b));
// modulo operator gives remainder on dividing first operand with second
             System.out.println("a % b = "+(a % b));
// if denominator is 0 in division then Arithmetic exception is thrown. Uncommenting below line would throw an exception
// System.out.println(a/c);
         }
   }

Output
a+b = 30
a-b = 10
x+y = ThankYou
a*b = 200
a/b = 2
a%b = 0

b. Unary Operators in Java

Operator NameDescription
Unary Minus (-)For decreasing the value
Unary Plus (+)For converting the negative values into positive
Increment operator (++)Used for increasing of the operand by 1
Post-IncrementThe value is incremented first then the result is computed
Pre-IncrementThe value is incremented later then the result is computed
Decrement OperatorUsed for decreasing the value of operand by 1
Post-DecrementThe value is decremented first then the result is computed
Pre-DecrementThe value is decremented later then the result is computed
Logical not Operator (!) Used for inverting the values of boolean

Example

// Java program to illustrate unary operators
public class operators
  {
      public static void main(String[] args)
          {
             int a = 20, b = 10, c = 0, d = 20, e = 40, f = 30;
           boolean condition = true;
// pre-increment operator
// a = a+1 and then c = a;
           c = ++a;
           System.out.println("Value of c (++a) = " + c);
// post increment operator
// c=b then b=b+1
           c = b++;
           System.out.println("Value of c (b++) = " + c);
// pre-decrement operator
// d=d-1 then c=d
           c = --d;
           System.out.println("Value of c (--d) = " + c);
// post-decrement operator
// c=e then e=e-1
           c = --e;
           System.out.println("Value of c (--e) = " + c);
// Logical not operator
           System.out.println("Value of !condition =" + !condition);
         }
    }

Do you know What’s New in Java 10?

c.  Assignment Operators in Java

Assignment operators are used to assigning values to the left operand. Its types are,

Operator NameDescription
+=To add the right and left operator and then assigning the result to the left operator
-=To subtract the two operands on left and right and then assign the value to the left operand
*=To multiply the two operands on left and right and then assign the value to the left operand
/=To divide the two operands on left and right and then assign the value to the left operand
^=To raise the value of left operand to the power of right operator
%=To apply modulo operator

Example-

// Java program to illustrate assignment operators
public class operators
  {
     public static void main(String[] args)
        {
           int a = 20, b = 10, c, d, e = 10, f = 4, g = 9;
// simple assignment operator
           c = b;
           System.out.println("Value of c = " + c);
// This following statement would throw an exception as value of right operand must be initialised before an assignment, and the program would not compile.
// c = d;
// instead of below statements,shorthand assignment operators can be used to provide same functionality.
           a = a + 1;
           b = b - 1;
           e = e * 2;
           f = f / 2;
System.out.println("a,b,e,f = " + a + ", + b + "," + e + "," + f);
           a = a - 1;
           b = b + 1;
           e = e / 2;
           f = f * 2
// shorthand assignment operator
           a += 1;
           b -= 1;
           e *= 2;
           f /= 2;
System.out.println("a,b,e,f (using shorthand operators)= " +
           a + "," + b + "," + e + "," + f);
       }
  }

d. Relational Operators in Java

Relational Java  operators are used to check the equality and for comparison.

Operator NameDescription
== (equals to) ex. x==yTrue if x equals y, otherwise false
!= (not equal to) ex. x!=yTrue if x is not equal to y, otherwise false
< (less than) ex. x<yTrue if x is less than y, otherwise false
> (greater than) ex.x > yTrue if x is greater than y, otherwise false
>= (greater than or equal to)  ex. x>=yTrue if x is greater than or equal to y, otherwise false
<= (less than or equal to) ex. x<=yTrue if x is less than or equal to y, otherwise false

Let’s explore the Career Opportunities in Java Programming

Example
// Java program to illustrate relational operators

public class operators
{
public static void main(String[] args)
{
int a = 20, b = 10;
String x = "Thank", y = "Thank";
int ar[] = { 1, 2, 3 };
int br[] = { 1, 2, 3 };
boolean condition = true;
//various conditional operators
System.out.println("a == b :" + (a == b));
System.out.println("a < b :" + (a < b));
System.out.println("a <= b :" + (a <= b));
System.out.println("a > b :" + (a > b));
System.out.println("a >= b :" + (a >= b));
System.out.println("a != b :" + (a != b));
// Arrays cannot be compared with relational operators because objects store references not the value
System.out.println("x == y : " + (ar == br));
System.out.println("condition==true :" + (condition == true));
}
}

e. Logical operators in Java

Operator NameDescription
&& (Logical AND)Returns the value if both the conditions are true otherwise returns zero.
II (Logical OR)Returns the value if even one condition is true

Example
// Java program to illustrate logical operators

public class operators
{
public static void main(String[] args)
{
String x = "Sher";
String y = "Locked";
Scanner s = new Scanner(System.in);
System.out.print("Enter username:");
String uuid = s.next();
System.out.print("Enter password:");
String upwd = s.next();
// Check if user-name and password match or not.
if ((uuid.equals(x) && upwd.equals(y)) ||
(uuid.equals(y) && upwd.equals(x))) {
System.out.println("Welcome user.");
}
else
{
System.out.println("Wrong uid or password");
}
}
}

f. Ternary Operators in Java

Ternary java operators are a shorthand version the ‘if else’ statements.
Syntax- condition ? if true : if false
Example
// Java program to illustrate max of three numbers using ternary operator.

public class operators
{
public static void main(String[] args)
{
int a = 20, b = 10, c = 30, result;
//result holds max of three numbers
result = ((a > b) ? (a > c) ? a :
c : (b > c) ? b : c);
System.out.println("Max of three numbers = "+result);
}
}

Follow this link to know about Popular Java Applications

g.  Bitwise Operators in Java

Bitwise java operators are used to perform operations on single bitwise values.

Operator NameDescription
Java Bitwise AND operator (&)The & operator compares corresponding bits of two operands. If both bits are 1, it gives 1 else 0.
Java Bitwise OR operator (|)The | operator compares corresponding bits of two operands. If either of the bits is 1, it gives 1 else 0.
Bitwise XOR operator (^)The ^ operator compares corresponding bits of two operands. If corresponding bits are different, it gives 1 else 0.
Bitwise Complement operator (~)The ~ operator inverts the bit pattern. It makes every 0 to 1 and every 1 to 0.

Before discussing the example, let’s discuss Java Data types and Java Access Modifiers.
Example
// Java program to illustrate bitwise operators

public class operators
{
public static void main(String[] args)
{
int a = 0x0005;
int b = 0x0007;
// bitwise and
// 0101 & 0111=0101
System.out.println("a&b = " + (a & b));
// bitwise OR
// 0101 | 0111=0111
System.out.println("a|b = " + (a | b));
// bitwise xor
// 0101 ^ 0111=0010
System.out.println("a^b = " + (a ^ b));
// bitwise Complement
// ~0101=1010
System.out.println("~a = " + ~a);
// can also be combined with assignment operator to provide shorthand assignment
// a=a&b
a &= b;
System.out.println("a= " + a);
}
}

h. Shift Operators in Java

Operator NameDescription
<< (left shift operator)Shifts the value to the left which specified by the right operand.
>> ( right shift operator)Shifts the value by zeroes defined by left operand.
>>> (unsigned right shift operator)It fills the void on left with zeroes which are set by the right operand.

Example
// Java program to illustrate shift operators

public class operators
{
public static void main(String[] args)
{
int a = 0x0005;
int b = -10;
// left shift operator
// 0000 0101<<2 =0001 0100(20)
// similar to 5*(2^2)
System.out.println("a<<2 = " + (a << 2));
// right shift operator
// 0000 0101 >> 2 =0000 0001(1)
// similar to 5/(2^2)
System.out.println("a>>2 = " + (a >> 2));
// unsigned right shift operator
System.out.println("b>>>2 = "+ (b >>> 2));
}
}

So, this was all about Java Operators Tutorial. hope you like our explanation.

4.  Conclusion

In this tutorial for Java operators, we learned about the Java basic operators, their use and implementation of the Java codes. This will help us to code better and understand the codes better. Furthermore, if you have any query, feel free to ask in the comment box.

See Also – Decision Making in Java

For reference

7 Responses

  1. Sarika Bhutare says:

    Very nice study material.

    • Data Flair says:

      Hi Sarika,
      Thank you, for taking a part in our journey and commenting on our “Java Operators” Tutorial. We are always motivated after reading this kind of loyal feedbacks.
      Keep Visiting and Keep Learning.
      Regards,
      Data-Flair

  2. SURYA says:

    The best and helpful.

    • Data Flair says:

      Your Appreciation means a lot to us, we keep trying to give a detailed and advanced piece of information for our readers.
      “Education is the most powerful WEAPON” which you can use to CHANGE the WORLD”
      Regards,
      Data-Flair

  3. Amarnathreddy says:

    Good study material

    • Data Flair says:

      Hi Amarnathreddy,
      We are glad you comment on “Java Operators Tutorial”.
      Comments like these let us know we’re on the right track trying to deliver the best content to our readers. You should also check out our Java Quizl, to check your Java Knowledge.
      Regards,
      Data-Flair

  4. Divyansh says:

    Please provide all outputs

Leave a Reply

Your email address will not be published. Required fields are marked *