Java Operators Explained [Easy Examples]


JAVA

Author: Bashir Alam
Reviewer: Deepak Prasad

 

Introduction to Java Operators

Java Operators are an integral part of the Java programming language. They are used to perform different operations on data types and help us to build a logical system and solve complex real problems. There are mainly eight java operators. In this article, we will cover all these eight types along with taking different examples.

We will see how we can use these operators in java programming and what each of the operators does. All in all this tutorial is going to have all the details and explanations of all operators that are used in a java programming language.

 

Different Types of Java Operators

Java operators are some special symbols that are used to perform mathematical and logical operations on the operands and return the result. There are several types of operators in java that are used to perform different operations. For example, the  (-) operator symbol is used to subtract two or more operands and returns the subtraction as a result. In the Java language, there are a total of eight operators. See the list below:

  • Arithmetic operators
  • Relational operators
  • Logical operators
  • Bitwise operators
  • Assignment operators
  • Shift operators
  • Unary operators
  • Ternary operators

In the upcoming sections, we will have a close look at all these different types of java operators.

 

Arithmetic Java Operators

Arithmetic java operators are used to perform different mathematical calculations. In java, there are five different types of arithmetic operators which perform different calculations.

They are addition, subtraction, multiplication, division, and the remainder operator. We use special symbols for these operations in the java programming language.

See the following list which shows some of these arithmetic operations symbols.

  • + ; used for the addition of numbers
  • - ; used for subtraction of numbers
  • * ; used for multiplication of numbers
  • / ; used for division of numbers
  • % ; used to find the modulus of a number

Now let us take an example that uses all these five operators. See the example below:

// class
public class Main  { 

   // main method
   public static void main(String[] args)  { 

       // variable defining
       int  a = 10;
       int b = 3;

       // adding the numbers
       System.out.println("Addition of two number is :"+ (a+b));

       // subtracting the numbers
       System.out.println("subtraction of two number is :"+ (a-b));

       // multipication of the numbers
       System.out.println("Product of two number is :"+ (a*b));

       // division of the numbers
       System.out.println("division of two number is :"+ (a/b));

       // modulus of the number
       System.out.println("Modulus the number is :"+ (a%b)); 
    } 
}

Output:

Addition of two number is :13
subtraction of two number is :7
Product of two number is :30
division of two number is :3
Modulus the number is :1

 

Relational Java Operators

Relational operator refers to the relationships that values or operands can have with one another. Java provides 6 relational operators for comparing numbers and characters. They are mostly used for comparison purposes. After the comparison, they return the result in boolean datatype.

If the comparison is true, the relational operator results in true, otherwise false. They are extensively used in looping as well as conditional if-else statements.

The following list shows some of those operators and their uses. See the list below:

  • == : Return true if both the operands are equal.
  • != ; Return true if both the operands are not equal.
  • < ; Return true if left-hand side operand is less than right side one.
  • > ; Return true if right-hand side operand is less than left side one.
  • <= ; Return true if left side operand is less than or equal to right side one.
  • >= ; Return true if right side operand is less than or equal to left side one.

 

Now let us take an example and see how we can use them in a java programming language. See the example below:

// class
public class Main  { 

   // main method
   public static void main(String[] args)  { 

       // equal java operators
       System.out.println("Equal operator returns :"+ (10==10));

       // Not equal java operators
       System.out.println("Not equal operator returns:"+ (10!=10));

       // less than java operators
       System.out.println("Less than operator :"+ (10<10));

       // Greater than java operators
       System.out.println("Greater than operator :"+ (10>10));

       // less than or equal to java operators
       System.out.println("Less than or equal to operator returns :"+ (10<=10));

       // greater than or equal to java operators
       System.out.println("Greater than or equal to operator returns:"+ (10>=10)); 
    } 
}

Output:

Equal operator returns :true
Not equal operator returns:false
Less than operator :false
Greater than operator :false
Less than or equal to operator returns :true
Greater than or equal to operator returns:true

 

Logical Java Operators

Logical operators are also known as conditional operators. These operators are used for evaluating one or more boolean expressions and for complex decision-making. They also return a boolean value (true or false). There are three different logical operators in java. See the list below which shows the three logical java operators.

  • && ; Return true if both the conditions are true.
  • || ; Return true if any of the conditions are true.
  • ! ; Return true if the condition is not true

Now let us take an example and see how we can use them and how are actually working in java. See the example below:

// class
public class Main  { 
   // main method
   public static void main(String[] args)  { 
       // and operator operators
       System.out.println("And operator returns :"+ ((10==10) && (1==3)));

       // Or java operators
       System.out.println("OR operator returns  :"+ ((10==10)||(1==2)));

       //NOT java operators
       System.out.println("NOT operator returns :"+ !(10==10));
   } 
} 

Output:

And operator returns :false
OR operator returns :true
NOT operator returns :false

Notice that the AND operator returns false because the second condition was false. The OR operator returns true because the first condition was true and the NOT operator returns false even the condition was true because it is the opposite of the condition.

 

Bitwise Java Operators

The Bitwise java operators manipulate the individual bits of a number. The bitwise operators work with the integer types that is, byte, short, int, and long types. Java provides 4 bitwise operators which are list below:

  • & :  Only 1 AND 1 produces 1, any other combination will produce 0
  • | :  Only 0 OR 0 produces 0, any other combination will produce 1.
  • ^: 1 XOR 0 produces 1 and similarly 0 XOR 1 also produces 1, any other combination will produce 0
  • ~ :  Used for complement operations.

Now let us take examples of each of these operators and see how they work in the Java programming language.

 

Examples of bitwise AND,  OR  and XOR operators

Bitwise OR is similar to logical OR. It returns zero if both the inputs are 0, else it returns 1.  Now let us take a simple example and see how it works. See the example below:

// class
public class Main  { 

   // main method
   public static void main(String[] args)  { 

       // bitwie Or operator
       System.out.println((12==3)|(12==12));
   } 
}

Output:

true

Similarly, the bitwise AND operator return 1 true if both the operands are true, else it returns 0. See the example below:

// class
public class Main  { 

   // main method
   public static void main(String[] args)  { 

       // bitwise AND operator
       System.out.println((12==3)&(12==12));
   } 
}

Output:

false

Now let us take an example of XOR operator and see how it works. See the example below:

// class
public class Main  { 

   // main method
   public static void main(String[] args)  { 

       // bitwie XOR operator
       System.out.println((1==1)^(2==2));
   } 
} 

Output:

false

Notice that we get false because XOR return 0 or false when both the conditions are true.

 

Example of Java bitwise negation

A bitwise negation performs logical negation on a given number by flipping all of its bits. The inverted bits are a complement to one, which turns zeros into ones and ones into zeros. Now let us take an example and see what happens when we apply a bitwise operation over it. See the example below.

// class
public class Main  { 

   // main method
   public static void main(String[] args)  { 

       // bitwie negation
       System.out.println(~(12));
   } 
}

Output:

-13

 

Shift Java Operators

A shift java operator performs bit manipulation on operands by shifting the bits of its first operand to right or left. Java supports three types of shift operators which are listed below:

  • Signed Left shift (<<):  The signed left shift operator shifts the bits of the number or operand to the left and fills 0 on vacated bits left as a result.
  • Signed Right shift (>>): The signed right shift operator shifts the bits of the number to the right
  • Unsigned Right shift (>>>>) : The unsigned right shift operator shifts the bits of the number to the right. In an unsigned right shift operation, the leftmost vacated bits are always set to 0.

Now let us take an example and see how each of these operands works. See the example below:

// class
public class Main  { 

   // main method
   public static void main(String[] args)  { 
       int num = 100;
       int newNum;
       // left shift
       newNum = num << 3;
       System.out.println("num1 << 3: "+newNum);

       // signed right shift
       newNum = num >> 3;
       System.out.println("num1 >> 3: "+newNum);

       // unsigned right shift
       newNum = num >>> 3;
       System.out.println("num1 >>> 3: "+newNum);
   } 
}

Output:

num1 << 3: 800
num1 >> 3: 12
num1 >>> 3: 12

Now let us explain what happens in the above example. The binary form of 100 is 1100100 and after 3 left shift it becomes 1100100000 and when we convert it back to decimal, it is equal to 800. In a similar way, when we apply the right shift on 1100100 it becomes 0100 which is nothing but 12 in decimal number.

 

Assignment Java operator

Like other programming languages, Java also has an assignment operator which is an equal (=) sign.  Assignment Operator follows the right to left associativity, that is a value given on the right-hand side of the operator is assigned to the variable on the left-hand side and therefore we should declare the value of the right-hand side before using it.

We already have used the assignment operator in our previous examples a lot. Now let us take another example of an assignment operator and see how it works in java. See the example below:

// class
public class Main  { 

   // main method
   public static void main(String[] args)  { 

       // assignment operator
       int num = 100;
       System.out.println("Assigned value is: "+ num);
   } 
}

Output:

Assigned value is: 100

Notice that in the above example, we assigned a value of 100 to the variable num by using the assignment operator.

 

Unary Java Operators

The operators that act on one operand are called Unary Operators. They are mostly used for increment and decrement purposes. The simple syntax looks like this;

// pre-increment and pre-decrement
++num;
--num;

// post increment and post decrement
num ++; 
num --;

Let us take the example of both increment and decrement and see how they are working. See the example of increment.

// class
public class Main  { 

   // main method
   public static void main(String[] args)  { 

       // variables
       int num1 = 100;
       int num2 = 200;
       // pre increment and decrement
       System.out.println("Pre-increment: "+ (++num1));
       System.out.println("Pre-decrement: "+(--num2));

   } 
}

Output:

Pre-increment: 101
Pre-decrement: 199

Notice that we get 101 and 199 because pre-increment or pre-decrement first performs the incrementation or decrementation on operand and then calls it.

But in the case of post one, it is the opposite. See the example below:

// class
public class Main  { 

   // main method
   public static void main(String[] args)  { 

       // variables
       int num1 = 100;
       int num2 = 200;

       // pre increment and decrement
       System.out.println("Post-increment: "+ (num1++));
       System.out.println("Post-decrement: "+(num2--));
   } 
}

Output:

Post-increment: 100
Post-decrement: 200

Notice that the output didn't change it is because, in the post-increment or decrement, the operation is performed after calling the operands. If we print the same operand again, we will see the incrementation and decrementation.

 

Ternary Java Operator

Java offers a shortcut conditional operator (?:) that stores a value according to the condition. This operator is a ternary operator that is it requires three operands.  The simple syntax of the ternary operator looks like this:

expression1 ? expression2 : expression3 ;

The result of the whole expression depends on the value of expression1. If expression1 evaluates to true that is 1, then the value of expression2 is evaluated, otherwise, the value of expression3 is evaluated.

Let us now take an example and see how it works. See the example below:

// class
public class Main  { 

   // main method
   public static void main(String[] args)  { 

       // ternary operator
       System.out.println(6>4 ? "pass": "fails");
       System.out.println(6 <4 ? "pass": "fails");
   } 
}

Output:

pass
fails

Notice that we get a pass for the first case because there the first expression was true so the second expression was evaluated. While in for the second one we get fails because the first expression was false so the last expression was evaluated.

 

Summary

Operators are an important part of any programming language because they allow us to perform the complex calculations. Java supports eight different types of operators which include arithmetic, logical, assignment, bitwise, ternary, rational, shift wise and unary operators. In this tutorial, we covered all these different operators in detail. We learned how we can use each of them in the Java programming language and came across various examples. All in all, this tutorial, covers everything that you need to know in order to have a solid command over java operators.

 

Further Reading

Java operators
Java operators documentation
More about java operations

 

Bashir Alam

Bashir Alam

He is a Computer Science graduate from the University of Central Asia, currently employed as a full-time Machine Learning Engineer at uExel. His expertise lies in Python, Java, Machine Learning, OCR, text extraction, data preprocessing, and predictive models. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment