🅾️Operators

Java Operators: Meaning and Types Explained

⭐️ Java Operators Cheatsheet ⭐️

Assignment Operators 👉

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

Arithmetic Operators ➕➗

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

Increment & Decrement Operators 📈📉

++ // Increment
-- // Decrement 

Relational Operators ♻️

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

Logical Operators ✅❌

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

Bitwise Operators 👁‍🗨

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

Ternary Operator ❓

condition ? expression1 : expression2

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?

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.

Major Types of Operators in Java

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

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.

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.

int a = 10;
int b = 20;

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

Multiplication (*)

The multiplication operator (*) multiplies two numeric operands.

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.

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.

int a = 25;
int b = 4;

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

Ternary Operators in Java

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

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

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

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

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

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 (==)

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 (!=)

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 (>)

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

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

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

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

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

Used to increase (or decrease) the value of a variable by 1

Easy to use, important to recognize

The increment operator

count++;
++count;

The decrement operator

count--;
--count;

INCREMENT AND DECREMENT OPERATORS: TWO OPTIONS

Post-Increment

count++;

Uses current value of variable, THEN increments it

Pre-Increment

++count;

Increments variable first, THEN uses new value

Equivalent Operations

count++;
++count;
count = count + 1;

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

INCREMENT AND DECREMENT OPERATORS IN EXPRESSIONS

after executing

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

result = 15

m=5

after executing

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

result = 12

m=5

Assignment Operators in Java

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

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

+ (Addition)

Adds values on either side of the operator.

A + B will give 30

- (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 200

/ (Division)

Divides left-hand operand by right-hand operand.

B / A will give 2

% (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 21

-- (Decrement)

Decreases the value of operand by 1.

B-- gives 19

Last updated