blog

Home / DeveloperSection / Blogs / Understanding Java Operators

Understanding Java Operators

Understanding Java Operators

Ravi Vishwakarma 106 03-Jun-2024

Operators in Java are special symbols or keywords used to perform operations on variables and values. They form the basis of all computations and logical operations in a Java program. Operators in Java can be categorized into several types based on their functionality:

1. Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations.

Operator Description Example
+ Addition a + b
- Subtraction a - b
* Multiplication a * b
/ Division a / b
% Modulus (remainder) a % b

2. Unary Operators

Unary operators require only one operand and perform various operations such as incrementing/decrementing a value by one, negating an expression, or inverting the value of a boolean.

Operator Description Example
+ Unary plus (positive value) +a
- Unary minus (negation) -a
++ Increment ++a or a++
-- Decrement --a or a--
! Logical NOT !booleanValue

3. Assignment Operators

Assignment operators are used to assign values to variables.

Operator Description Example
= Assign a = b
+= Add and assign a += b (same as a = a + b)
-= Subtract and assign a -= b (same as a = a - b)
*= Multiply and assign a *= b (same as a = a * b)
/= Divide and assign a /= b (same as a = a / b)
%= Modulus and assign a %= b (same as a = a % b)

4. Relational Operators

Relational operators are used to compare two values. They return a boolean result.

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

5. Logical Operators

Logical operators are used to combine multiple boolean expressions.

Operator Description Example
&& Logical AND a && b
`   `
! Logical NOT !a

6. Bitwise Operators

Bitwise operators are used to perform bit-level operations on integral types.

Operator Description Example
& Bitwise AND a & b
` ` Bitwise OR
^ Bitwise XOR a ^ b
~ Bitwise complement ~a
<< Left shift a << 2
>> Right shift a >> 2
>>> Unsigned right shift a >>> 2

7. Ternary Operator

The ternary operator is a shorthand for an if-else statement. It takes three operands.

Operator Description Example
? : Ternary (conditional) condition ? value1 : value2

8. Instanceof Operator

The instanceof operator is used to test whether an object is an instance of a specific class or interface.

Operator Description Example
instanceof Checks instance object instanceof ClassName

Example Code

Here's an example demonstrating various operators in Java:

public class OperatorsExample {
    public static void main(String[] args) {
        int a = 10, b = 5;

        // Arithmetic Operators
        System.out.println("a + b = " + (a + b)); // Addition
        System.out.println("a - b = " + (a - b)); // Subtraction
        System.out.println("a * b = " + (a * b)); // Multiplication
        System.out.println("a / b = " + (a / b)); // Division
        System.out.println("a % b = " + (a % b)); // Modulus

        // Unary Operators
        System.out.println("a++ = " + (a++)); // Post-increment
        System.out.println("++a = " + (++a)); // Pre-increment
        System.out.println("a-- = " + (a--)); // Post-decrement
        System.out.println("--a = " + (--a)); // Pre-decrement

        // Assignment Operators
        a += b; // Equivalent to a = a + b
        System.out.println("a += b = " + a);
        a -= b; // Equivalent to a = a - b
        System.out.println("a -= b = " + a);

        // Relational Operators
        System.out.println("a == b: " + (a == b));
        System.out.println("a != b: " + (a != b));
        System.out.println("a > b: " + (a > b));
        System.out.println("a < b: " + (a < b));

        // Logical Operators
        boolean x = true, y = false;
        System.out.println("x && y: " + (x && y));
        System.out.println("x || y: " + (x || y));
        System.out.println("!x: " + (!x));

        // Ternary Operator
        int max = (a > b) ? a : b;
        System.out.println("Max of a and b is: " + max);

        // instanceof Operator
        String str = "Hello";
        boolean result = str instanceof String;
        System.out.println("Is str an instance of String? " + result);
    }
}

Summary

Java operators are used to perform various operations on variables and values. Understanding these operators is essential for performing computations, making decisions, and controlling the flow of Java programs. Each type of operator serves a specific purpose, from arithmetic operations to logical decisions and bit-level manipulations.

 

Read more - 

Overview of Java Scanner Class


Updated 03-Jun-2024
Hi, my self Ravi Vishwakarma. I have completed my studies at SPICBB Varanasi. now I completed MCA with 76% form Veer Bahadur Singh Purvanchal University Jaunpur. SWE @ MindStick | Software Engineer | Web Developer | .Net Developer | Web Developer | Backend Engineer | .NET Core Developer

Leave Comment

Comments

Liked By