# Operators

## Java Operators: Meaning and Types Explained <a href="#headline-139-647" id="headline-139-647"></a>

<div data-full-width="true"><figure><img src="https://herovired.com/wp-content/uploads/2023/04/Java-Operators-Meaning-and-Types-01.webp" alt=""><figcaption></figcaption></figure></div>

<details>

<summary>⭐️ Java Operators Cheatsheet ⭐️</summary>

**Assignment Operators 👉**

```java
=  // Assign value
+= // Add and assign 
-= // Subtract and assign
*= // Multiply and assign 
/= // Divide and assign
%= // Modulus and assign
```

**Arithmetic Operators ➕➗**

```java
+ // Add 
- // Subtract
* // Multiply
/ // Divide
% // Modulus (remainder) 
```

**Increment & Decrement Operators 📈📉**

```java
++ // Increment
-- // Decrement 
```

**Relational Operators ♻️**

```java
== // Equal to
!= // Not equal to 
>  // Greater than
<  // Less than
>= // Greater than or equal to
<= // Less than or equal to
```

**Logical Operators ✅❌**

```java
&& // Logical AND
|| // Logical OR
!  // Logical NOT
```

**Bitwise Operators 👁‍🗨**

```java
&  // Bitwise AND
|  // Bitwise OR
^  // Bitwise XOR
~  // Bitwise complement
<< // Left shift
>> // Right shift
>>> // Unsigned right shift
```

**Ternary Operator ❓**

```java
condition ? expression1 : expression2
```

</details>

While learning Java, you will come across the concept of operators. What is a Java operator? Java operators are symbols useful for performing operations on variables. They also help manipulate the values of the operands.

The different Java operators are useful for performing various functions. Dive into this article to learn more about Java operators, their types, and their importance.

### Why are Operators Important in Java Programming? <a href="#operators" id="operators"></a>

Once you know what is a Java operator, you must consider its importance. You should know that operators in Java are extremely vital. Without Java operators, you won’t be able to make logical, arithmetic calculations in different programs.\
\
According to the type of Java operators, they manipulate a logical or arithmetic value or operand in a certain way to deliver a specific outcome. Operators in Java play a crucial role in the programming world by dealing with simple arithmetic functions and executing complex algorithms. Java operators are also responsible for key functions like security encryption.&#x20;

<div data-full-width="true"><figure><img src="https://herovired.com/wp-content/uploads/2023/04/Java-Operators-Meaning-and-Types-02.webp" alt=""><figcaption></figcaption></figure></div>

### Major Types of Operators in Java  <a href="#major" id="major"></a>

Now that you know what is a Java operator, you should learn about the different types of operators in Java.

* Arithmetic Operators
* Ternary Operators
* Bitwise Operator
* Relational Operators
* Logical Operators
* Unary Operators
* Assignment Operator
* Shift Operator in Java

## Arithmetic Operators in Java <a href="#arithmetic" id="arithmetic"></a>

Arithmetic operators in Java are useful for executing addition, multiplication, division, subtraction, and modulus. As you can see, these operators in Java are valuable for mathematical operations.

**Examples of Arithmetic Operators**

**Addition (+)**

The addition operator (+) is used to add two operands. The operands can be numeric, string or a combination of both.

```java
int a = 10;
int b = 20;

// Adding two integers
int sum = a + b; // sum = 30

// Adding integer and string
String str = "Hello " + a; // str = "Hello 10"
```

When adding numbers, it will perform numerical addition. When adding strings, it will concatenate them. Adding a number and string will convert the number to a string and concatenate.

**Subtraction (-)**

The subtraction operator (-) subtracts the right operand from the left operand. Both operands must be numeric.

```java
int a = 10;
int b = 20;

int diff = a - b; // diff = -10
```

**Multiplication (\*)**

The multiplication operator (\*) multiplies two numeric operands.

```java
int a = 10; 
int b = 20;

int product = a * b; // product = 200
```

**Division (/)**

The division operator (/) divides the left operand by the right one. It performs floating point or integer division depending on the operands.

```java
int a = 20;
int b = 10;

// Integer division
int result1 = a / b; // result1 = 2 

// Floating point division
float result2 = a / (float)b; // result2 = 2.0
```

**Modulus (%)**

The modulus operator (%) returns the remainder left over after dividing the left operand by the right. Both operands must be integers.

```java
int a = 25;
int b = 4;

int remainder = a % b; // remainder = 1 (25 / 4 = 6 remainder 1)
```

## Ternary Operators in Java <a href="#ternary" id="ternary"></a>

**What is the ternary operator in Java?**

The ternary operator in Java is a shorthand way of writing an `if-else` statement. It allows you to evaluate a condition and execute one block of code if the condition is true, and another block of code if the condition is false.

**Syntax**

```java
condition ? expressionIfTrue : expressionIfFalse;
```

The syntax consists of three parts:

* The condition to evaluate which must return a boolean value
* The expression to execute if the condition is true followed by a question mark `?`
* The expression to execute if the condition is false followed by a colon `:`

**Example**

```java
int num = 10;
String result = num > 5 ? "Greater than 5" : "Less than or equal to 5";

// result will contain "Greater than 5"
```

Here we evaluate if the variable `num` is greater than 5. If true, we set `result` to "Greater than 5", otherwise we set it to "Less than or equal to 5".

**Advantages**

* More concise syntax compared to if-else statements
* Improves readability when used for simple conditional checks

The ternary operator provides a shorthand way to write simple conditional logic in a concise, easy to read manner.

## Bitwise Operator in Java <a href="#bitwise" id="bitwise"></a>

The bitwise operator in Java operates on a bit array, bit string, or binary number. The fast and simple operator in Java is directly supported by the processor. You can also refer to the bitwise operation as bit-level programming.

**Examples of Bitwise Operators in Java**

```java
public class Herovired {
public static void main( String[] args ) {
            int a = 58; 
            int b = 13; 
        	System.out.println ( a&b );  
       	        System.out.println ( a|b );  
        	System.out.println ( a^b );  
        	System.out.println ( ~a );  
	}
	}
```

## Relational Operators in Java <a href="#relational" id="relational"></a>

**What are relational operators?**

Relational operators are used to compare two values and determine the relationship between them. They evaluate to a boolean result of either true or false.

**Types of relational operators**

* `==` - Equal to
* `!=` - Not equal to
* `>` - Greater than
* `<` - Less than
* `>=` - Greater than or equal to
* `<=` - Less than or equal to

**Equal to (==)**

```java
int a = 5;
int b = 5;

boolean result = a == b; // true
```

Compares if two values are equal. The result is true if they are equal, false otherwise.

**Not equal to (!=)**

```java
int a = 5; 
int b = 3;

boolean result = a != b; // true
```

Compares if two values are not equal. The result is true if they are not equal, false otherwise.

**Greater than (>)**

```java
int a = 5;
int b = 3; 

boolean result = a > b; // true
```

Compares if the left operand is greater than the right operand. The result is true if left is greater, false otherwise.

The other relational operators work similarly to compare the relationship between two values.

## Logical Operators in Java <a href="#logical" id="logical"></a>

Logical operators in Java help combine two or more conditions. It is useful for performing the original condition under consideration.

**Examples of Logical Operators in Java**

```java
public class Herovired {
public static void main ( String args[] ) {
             int a = 5;
             System.out.println ( a<5 && a<20 );
             System.out.println ( a<5 || a<20 );
             System.out.println ( ! ( a<5 && a<20 ));
}  
}
```

## Unary Operators in Java <a href="#unary" id="unary"></a>

The unary operators in Java demand a single operand. These Java operators can increase or decrease the value. As a result, an expression becomes negative, or the boolean value gets inverted.

**Examples of Unary Operators in Java**

```java
public class Herovired {
public static void main ( String[] args ) {  
int a = 10;  
boolean b = True;
System.out.println ( a++ );
System.out.println ( ++a );
System.out.println ( a-- );  
System.out.println ( --a );
System.out.println ( !b );
}
}
```

### INCREMENT AND DECREMENT OPERATORS&#x20;

Used to increase (or decrease) the value of a variable by 1&#x20;

Easy to use, important to recognize&#x20;

#### The increment operator&#x20;

```java
count++;
++count;
```

#### The decrement operator&#x20;

```java
count--;
--count;
```

### INCREMENT AND DECREMENT OPERATORS: TWO OPTIONS&#x20;

#### Post-Increment&#x20;

```java
count++;
```

Uses current value of variable, THEN increments it&#x20;

#### Pre-Increment&#x20;

```java
++count;
```

Increments variable first, THEN uses new value&#x20;

#### Equivalent Operations&#x20;

```java
count++;
++count;
count = count + 1;

count--;
--count;
count = count - 1;
```

### INCREMENT AND DECREMENT OPERATORS IN EXPRESSIONS&#x20;

#### after executing&#x20;

```java
int m = 4;
int result = 3 * (++m);
```

result = 15

m=5

#### after executing&#x20;

```java
int m = 4;
int result = 3 * (m++);
```

result = 12

m=5

## Assignment Operators in Java <a href="#assignment" id="assignment"></a>

The assignment operators in Java are beneficial for allocating new values to a variable. In assignment operators in Java, the left side is called a variable. The right side of these Java operators is called value.

**Examples of Assignment Operators in Java**

```java
public class Herovired {
public static void main ( String[] args ) {
int a = 10;
int b = 20;
int c;
System.out.println ( c = a ); 
System.out.println ( b += a );
System.out.println ( b -= a);
System.out.println ( b *= a );
System.out.println ( b /= a );
System.out.println ( b ^= a );
System.out.println ( b %= a );
}
}
```

<table data-full-width="true"><thead><tr><th>Operator</th><th>Description</th><th>Example</th></tr></thead><tbody><tr><td>+ (Addition)</td><td>Adds values on either side of the operator.</td><td>A + B will give 30</td></tr><tr><td>- (Subtraction)</td><td>Subtracts right-hand operand from left-hand operand.</td><td>A - B will give -10</td></tr><tr><td>* (Multiplication)</td><td>Multiplies values on either side of the operator.</td><td>A * B will give 200</td></tr><tr><td>/ (Division)</td><td>Divides left-hand operand by right-hand operand.</td><td>B / A will give 2</td></tr><tr><td>% (Modulus)</td><td>Divides left-hand operand by right-hand operand and returns remainder.</td><td>B % A will give 0</td></tr><tr><td>++ (Increment)</td><td>Increases the value of operand by 1.</td><td>B++ gives 21</td></tr><tr><td>-- (Decrement)</td><td>Decreases the value of operand by 1.</td><td>B-- gives 19</td></tr></tbody></table>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://qatesting.gitbook.io/qa/java/operators.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
