  Get Started: Java Operators

Java Operators – Before moving ahead, let’s know a bit about Java Type Casting.

### Java Operators

Java operators are used to perform operations on variables and values.

### Types of operators in Java

• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Bitwise operators

Let’s get started with one-by-one –

#### Arithmetic operators

Arithmetic operators are used to perform mathematical operations on numbers.

 Name Operator Description Example Addition + Adds together two values a+b Subtraction – Subtracts one value from another a-b Multiplication * Multiplies two values a*b Division / Divides one value by another a/b Modulus % Returns the division remainder a%b Increment ++ Increases the value of a variable by 1 a++b Decrement – – Decreases the value of a variable by 1 a–b

```				```
public class Main {
public static void main(String[] args) {
int a = 10;
int b = 20;
System.out.println(a+b);
}
}
```
```

Example: Subtraction

```				```
public class Main {
public static void main(String[] args) {
int a = 10;
int b = 20;
System.out.println(a-b);
}
}
```
```

Example: Multiplication

```				```
public class Main {
public static void main(String[] args) {
int a = 10;
int b = 20;
System.out.println(a*b);
}
}
```
```

Example: Division

```				```
public class Main {
public static void main(String[] args) {
int a = 20;
int b = 10;
System.out.println(a/b);
}
}
```
```

Example: Modulus

```				```
public class Main {
public static void main(String[] args) {
int a = 5;
int b = 2;
System.out.println(a%b);
}
}
```
```

Example: Increment

```				```
public class Main {
public static void main(String[] args) {
int a = 5;
++a
System.out.println(a);
}
}
```
```

Example: Decrement

```				```
public class Main {
public static void main(String[] args) {
int a = 5;
--a
System.out.println(a);
}
}
```
```

#### Java Assignment Operators

Java Assignment Operators are used to assign values to variables.

In the above examples, we have already used the assignment operator (=) to assign the value.

 Operator Example Same As = x = 1 x = 1 += x +=2 x = x+2 -= x +=3 x = x+3 *= x *=4 x = x*4 /= x /=4 x = x/4 %= x %=2 x = x%2 &= x &=2 x = x & 2 |= x |=2 x = x | 3 ^= x ^= 3 x = x ^ 3 >>= x >>= 2 x = x >> 2 <<= x <<= 2 x = x << 2

Example:

```				```
public class Main {
public static void main(String[] args) {
int a = 5;
System.out.println(a);
}
}
```
```

Example:

```				```
public class Main {
public static void main(String[] args) {
int a = 5;
a += 2;
System.out.println(a);
}
}
```
```

Example:

```				```
public class Main {
public static void main(String[] args) {
int a = 5;
a -= 2;
System.out.println(a);
}
}
```
```

Example:

```				```
public class Main {
public static void main(String[] args) {
int a = 5;
a *= 2;
System.out.println(a);
}
}
```
```

Example:

```				```
public class Main {
public static void main(String[] args) {
int a = 5;
a /= 2;
System.out.println(a);
}
}
```
```

Example:

```				```
public class Main {
public static void main(String[] args) {
int a = 5;
a %= 2;
System.out.println(a);
}
}
```
```

Example:

```				```
public class Main {
public static void main(String[] args) {
int a = 5;
a &= 2;
System.out.println(a);
}
}
```
```

Example:

```				```
public class Main {
public static void main(String[] args) {
int a = 5;
a |= 2;
System.out.println(a);
}
}
```
```

Example:

```				```
public class Main {
public static void main(String[] args) {
int a = 5;
a ^= 2;
System.out.println(a);
}
}
```
```

Example:

```				```
public class Main {
public static void main(String[] args) {
int a = 5;
a >>= 2;
System.out.println(a);
}
}
```
```

Example:

```				```
public class Main {
public static void main(String[] args) {
int a = 5;
a <<= 2;
System.out.println(a);
}
}
```
```

#### Java Comparison Operators

Comparison operators are used to compare two values:

 Name Operator Example Equal to == a==b Not equal != a!=b Greater than > a>b Less than < a= a>=b Less than or equal to <= a<=b

Example:

```				```
public class Main {
public static void main(String[] args) {
int a = 5;
int b = 2;
System.out.println(a == b);
}
}
```
```

Example:

```				```
public class Main {
public static void main(String[] args) {
int a = 5;
int b = 5;
System.out.println(a != b);
}
}
```
```

Example:

```				```
public class Main {
public static void main(String[] args) {
int a = 5;
int b = 4;
System.out.println(a > b);
}
}
```
```

Example:

```				```
public class Main {
public static void main(String[] args) {
int a = 5;
int b = 4;
System.out.println(a < b);
}
}
```
```

Example:

```				```
public class Main {
public static void main(String[] args) {
int a = 5;
int b = 4;
System.out.println(a >= b);
}
}
```
```

Example:

```				```
public class Main {
public static void main(String[] args) {
int a = 5;
int b = 4;
System.out.println(a <= b);
}
}
```
```

#### Java Logical Operators

Logical operators are used to determining the logic between variables or values.

 Name Operator Description Example && Logical and Return true if both statements are true x < 10 &&  x < 8 || Logical or Return true if one of the statements is true x < 2 || x < 4 ! Logical not Reverse the result, returns false if the result is true !(x < 10 && x < 5)

Example: Logical and

```				```
public class Main {
public static void main(String[] args) {
int x = 5;
System.out.println(x < 10 &&  x < 8);
}
}
```
```

Returns true because 5 is less than 10 AND 5 is less than 8.

Example: Logical or

```				```
public class Main {
public static void main(String[] args) {
int x = 5;
System.out.println(x < 2 || x < 4);
}
}
```
```

Returns true because one of the conditions is true (5 is not less than 3, but 5 is less than 4)

Example: Logical not

```				```
public class Main {
public static void main(String[] args) {
int x = 5;
System.out.println(!(x < 10 && x < 4));
}
}
```
```

Returns false because! (not) is used to reverse the result.

If you find anything incorrect in the above-discussed topic and have further questions, please comment below.

Connect on:

Share on