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

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

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

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.

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