Basic Operators In Java With Simple Examples

In this tutorial, we will talk about Basic Operators In Java. This is an important Java topic if you are making a system that uses basic math operators. You can use these basic operators to add, subtract, multiply, and divide numbers just like you would in a normal calculation. The best part is that we will talk about not just basic operators but all the Operators Used In Java so that you can learn them and use them in your next Java project.

What Is Basic Operators In Java

The Basic Operators In Java are symbols that do operations on variables and values. For example, + is an operator used for addition, while * is an operator used for multiplication.

How Many Types Of Operators

There are 6 types of operators in Java.

  • Arithmetic Operators
  • Relational Operators
  • Bitwise Operators
  • Logical Operators
  • Assignment Operators
  • Misc Operators
Python Tutorial 7 - Operators
Python Tutorial 7 - Operators

Without further ado, I will discuss all the types of Operators in Java.

Arithmetic Operators In Java

In mathematical expressions, Arithmetic Operators In Java are used in the same way that they are used in algebra. The math operators are shown in the table below.

If variable A has the value 20 and variable B has the value 30.

OperatorDescriptionExample
+ (Addition)Adds values on either side of the operator.A + B will give 50
– (Subtraction)Subtracts right-hand operand from left-hand operand.A – B will give -10
* (Multiplication)Multiplies values on either side of the operator.A * B will give 600
/ (Division)Divides left-hand operand by right-hand operand.B / A will give 0.6
% (Modulus)Divides left-hand operand by right-hand operand and returns remainder.B % A will give 0
++ (Increment)Increases the value of operand by 1.B++ gives 31
— (Decrement)Decreases the value of operand by 1.B– gives 29
Arithmetic Operators In Java

Relational Operators In Java

The Relational Operators In Java are used to check how two operands are related to each other. For example, / check if an is less than b: a b; Here, the relational operator is the symbol. It checks to see if an is smaller than b.

Let’s say variable A has 20 and variable B has 30.

OperatorDescriptionExample
== (equal to)Checks to see if the values of two operands are the same. If they are, condition is set to true.(A == B) is not true.
!= (not equal to)Checks if the values of two operands are equal or not. If the values are not equal, condition becomes true.(A != B) is true.
> (greater than)Checks if the value of the left operand is greater than the value of the right operand. If this is the case, condition becomes true.(A > B) is not true.
< (less than)Checks if the value of the left operand is less than the value of the right operand. If this is the case, condition becomes true.(A < B) is true.
>= (greater than or equal to)Checks to see if the value of the left operand is greater than or the same as the value of the right operand. If this is the case, condition becomes true.(A >= B) is not true.
<= (less than or equal to)Checks to see if the value of the left operand is less than or the same as the value of the right operand. If this is the case, condition becomes true.(A <= B) is true.
Relational Operators In Java

Bitwise Operators In Java

The Bitwise Operators In Java perform operations on integer data at the bit-level. Here, the integer data includes bytes, short, int, and long types of data. There are seven bit-level operators in Java. Operator.

Bitwise operator works with bits and does operations bit by bit. Suppose that an is 60 and b is 13. In binary, they will look like this:

a = 0011 1100

b = 0000 1101

————————-

a&b = 0000 1100

a|b = 0011 1101

a^b = 0011 0001

~a = 1100 0011

In the next table, you can see a list of the bitwise operators.

If variable A has the number 60 and variable B has the number 13.

OperatorDescriptionExample
& (bitwise and)If a bit is present in both operands, the AND operator copies it to the result.(A & B) will give 12 which is 0000 1100
| (bitwise or)If a bit is in both operands, the Binary OR Operator copies it.(A | B) will give 61 which is 0011 1101
^ (bitwise XOR)The Binary XOR Operator copies a bit if it is set in only one of the two operands.(A ^ B) will give 49 which is 0011 0001
~ (bitwise compliment)The Ones Complement Operator in binary is unary and “flips” bits.(~A ) will give -61 which is 1100 0011 in 2’s complement form due to a signed binary number.
<< (left shift)Left Shift Operator for Binary. The right operand tells how many bits to move the value of the left operand to the left.A << 2 will give 240 which is 1111 0000
>> (right shift)Right Shift Operator for Binary. The right operand tells how many bits to move the value of the left operand to the right.A >> 2 will give 15 which is 1111
>>> (zero fill right shift)Shift to the right and fill with zero. The number of bits given by the right operand is added to the value of the left operand. When values are moved, they are replaced with zeros.A >>>2 will give 15 which is 0000 1111
Bitwise Operators In Java

Logical Operators In Java

The Logical Operators In Java are used to check if an expression is true or false. They are used to make decisions.

Think of Boolean Variables. A is true, but variable B is false.

OperatorDescriptionExample
&& (logical and)named the logical AND operator. The condition is satisfied if both operands are non-zero.(A && B) is false
|| (logical or)known as the logical or operator The condition is met if either of the two operands is non-zero.(A || B) is true
! (logical not)Logical NOT Operator is a term. Use to change the operand’s operand’s logical state. The logical NOT operator will render a condition untrue if it is true.!(A && B) is true
Logical Operators In Java

Assignment Operators In Java

The Assignment Operators In Java are used in Java to give values to variables. For example, int age = 10; = is the assignment operator. It gives the value on its right to the variable on its left.

OperatorDescriptionExample
=Simple operator for assigning. Gives the values from the right operand to the left operand.C = A + B will assign value of A + B into C
+=Add AND to the list of operators. It adds the right operand to the left operand and assigns the result to the left operand.C += A is equivalent to C = C + A
-=Subtract AND the operator for assigning. It takes the difference between the right operand and the left operand and puts it in the left operand.C -= A is equivalent to C = C – A
*=Multiply AND the operator for assigning. It multiplies the right operand by the left operand and gives the result to the left operand.C *= A is equivalent to C = C * A
/=Divide AND the operator for assigning. It divides left operand by right operand and assigns the result to left operand.C /= A is equivalent to C = C / A
%=The assignment AND modulus operator. It uses two operands to find the modulus and puts the result in the left operand.C %= A is equivalent to C = C % A
<<=The assignment operator AND the left shift.C <<= 2 is same as C = C << 2
>>=Right shift AND an assignment operator.C >>= 2 is same as C = C >> 2
&=A bitwise AND assignment operator.C &= 2 is same as C = C & 2
^=Bitwise OR and the assignment operator.C ^= 2 is same as C = C ^ 2
|=The bitwise inclusive OR and assignment operator.C |= 2 is same as C = C | 2
Assignment Operators In Java

Miscellaneous Operators In Java

The MISC Operators In Java is the miscellaneous operator, which is a conditional operator with 3 operands. The first operand is always evaluated first. If not zero, the second operand is evaluated, and the value of that is the result. If not, the third operand is evaluated, and that is the value of the result.

There aren’t many other operators that the Java Language supports.

Conditional Operator ( ? : )

The ternary operator is another name for the conditional operator. This operator has three arguments and is used to check if a Boolean expression is true or false. The operator’s job is to figure out what value should be given to a variable.

variable g = (expression) ? value if true : value if false

Example Of Conditional Operator Code
// program by Glenn Magada Azuelo
public class Test {

   public static void main(String args[]) {
      int a, b;
      a = 100;
      b = (a == 1) ? 200: 300;
      System.out.println( "Value of b is : " +  b );

      b = (a == 100) ? 200: 300;
      System.out.println( "Value of b is : " + b );
   }
}
Output

Value of b is : 300
Value of b is : 200

In order for you to test your Java code provided in this lesson, you must test the code in your code editor. But if you wish to run this code online, we also have an online compiler in Java for you to test your java code for free.

We also provide a Java Online Compiler for every code below. Just scroll down so that you can easily test or execute the source code.


Instanceof Operator

This operator is used only with object reference variables. The operator checks whether the object is of a certain type (class type or interface type). The instanceof operator is written as

( Object reference variable ) instanceof (class/interface type)

If the object pointed to by the variable on the left side of the operator passes the IS-A check for the class/interface type on the right side, the result will be true.

Example Of instanceof Operator Code
// program by Glenn Magada Azuelo
public class Test {

   public static void main(String args[]) {

      String name = "Glenn Azuelo";

      // following will return true since name is type of String
      boolean result = name instanceof String;
      System.out.println( result );
   }
}
Output

true

This operator will still return true if the type of the object being compared matches the type on the right. Here’s one more example:

// program by Glenn Magada Azuelo
class Computer {}

public class Acer extends Computer {

   public static void main(String args[]) {

      Computer a = new Acer();
      boolean result =  a instanceof Acer;
      System.out.println( result );
   }
}
Output

true

Precedence Of Java Operators

The Operator Precedence tells how the terms in an expression are put together. This makes a difference in how an expression is judged. Some operators are more important than others. For example, the operator for multiplying is more important than the operator for adding.

For example, x = 7 + 3 * 2. In this case, x is 13, not 20, because * has a higher priority than +, so it is multiplied by 3 * 2 first and then added to 7.

Here, the operators with the most important roles are at the top of the table, and the ones with the least important roles are at the bottom. In an expression, the operators with the highest precedence will be evaluated first.

CategoryOperatorAssociativity
Postfixexpression++ expression–Left to right
Unary++expression –-expression +expression –expression ~ !Right to left
Multiplicative* / %Left to right
Additive+ –Left to right
Shift<< >> >>>Left to right
Relational< > <= >= instanceofLeft to right
Equality== !=Left to right
Bitwise AND&Left to right
Bitwise XOR^Left to right
Bitwise OR|Left to right
Logical AND&&Left to right
Logical OR||Left to right
Conditional?:Right to left
Assignment= += -= *= /= %= ^= |= <<= >>= >>>=Right to left
Precedence Of Java Operators

Conclusion

In this article, we talked about Basic Operators In Java, how it works, and how this chapter could help you learn Java. This could be one of the most important learning guides that will help you improve your skills and knowledge as a future Java Developer.

What’s Next

The next chapter will talk about loop control in Java programming. The chapter will talk about the different kinds of loops, how they can be used in Java program development, and what they are used for.


Leave a Comment