Basic Operators in Java With Simple Examples

What are the Basic Operators in Java

The Basic Operators In Java are symbols that do operations on variables and values.

For instance, + is an operator used for addition, while * is an operator used for multiplication.

You can use these basic operators to add, subtract, multiply, and divide numbers just like you would in a normal calculation.

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

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 a value of 20 and variable B has a value of 30.

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.

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. The 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.

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.

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.

Miscellaneous Operators In Java

The MISC operator 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 the Java code provided in this lesson, you must test the code in your code editor.

You can test the above example here! ➡Java Online Compiler

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

You can test the above example here! ➡Java Online Compiler

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 );
}
}```

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.