ChaPteR 4️⃣ Operators in Java Language

https://techyajay12.blogspot.com/2024/07/chapter-4-operators-in-java-language.html


Chapter 4️⃣ Operators in Java Language

 Introduction


Operators in Java are special symbols that perform specific operations on one, two, or three operands and then return a result. Understanding these operators is crucial for writing efficient and effective Java code.

 In this chapter, we will delve into the different types of operators available in Java, including arithmetic, assignment, comparison, logical, bitwise, conditional, and the `instanceof` operator.

Operators in Java Language


 4.1 Arithmetic Operators


Arithmetic operators are used to perform basic mathematical operations such as addition, subtraction, multiplication, division, and modulus. 
They work with both integer and floating-point data types.

- Addition (+):

Adds two operands.
  ```java
  int a = 5 + 3; // a = 8
  ```

https://techyajay12.blogspot.com/2024/07/chapter-4-operators-in-java-language.html


- Subtraction (-):

Subtracts the second operand from the first.
  ```java
  int b = 5 - 3; // b = 2
  ```

- Multiplication (*):

 Multiplies two operands.
  ```java
  int c = 5 * 3; // c = 15
  ```

- Division (/):

Divides the first operand by the second. Be mindful of division by zero.
  ```java
  int d = 10 / 2; // d = 5
  ```

- Modulus (%):

 Returns the remainder after dividing the first operand by the second.
  ```java
  int e = 10 % 3; // e = 1
  ```


 4.2 Assignment Operators


Assignment operators are used to assign values to variables. The most basic is the `=` operator, but there are also compound assignment operators that combine an arithmetic operation with assignment.

- Simple Assignment (=):

Assigns the right-hand operand to the left-hand operand.
  ```java
  int f = 5;
  ```

- Compound Assignments (+=, -=, *=, /=, %=): 


Perform an operation and then assign the result to the left-hand operand.
  ```java
  f += 3; // f = f + 3; f = 8
  ```
 4.3 Comparison Operators

Comparison operators are used to compare two values. They return a boolean result (`true` or `false`).

- Equal to (==):

Checks if two values are equal.
  ```java
  boolean result = (5 == 5); // true
  ```

- Not equal to (!=): 

Checks if two values are not equal.
  ```java
  boolean result = (5 != 3); // true
  ```

- Greater than (>):

Checks if the left-hand value is greater than the right-hand value.
  ```java
  boolean result = (5 > 3); // true
  ```

- Less than (<):

 Checks if the left-hand value is less than the right-hand value.
  ```java
  boolean result = (3 < 5); // true
  ```

- Greater than or equal to (>=):

Checks if the left-hand value is greater than or equal to the right-hand value.
  ```java
  boolean result = (5 >= 5); // true
  ```

- Less than or equal to (<=):

Checks if the left-hand value is less than or equal to the right-hand value.
  ```java
  boolean result = (3 <= 5); // true
  ```

👉👇 ALSO READ :-  👇

2.  Chapter  2️⃣ 👍

4.4 Logical Operators


Logical operators are used to combine multiple boolean expressions and return a boolean result.

- AND (&&):

 Returns true if both operands are true.
  ```java
  boolean result = (true && false); // false

https://techyajay12.blogspot.com/2024/07/chapter-4-operators-in-java-language.html


  ```

- OR (||):

 Returns true if at least one of the operands is true.
  ```java
  boolean result = (true || false); // true
  ```


- NOT (!):

 Returns true if the operand is false.
  ```java
  boolean result = (!true); // false
  ```


4.5 Bitwise Operators


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

- AND (&):

 Performs a bitwise AND operation.
  ```java
  int result = 5 & 3; // result = 1
  ```

- OR (|):

Performs a bitwise OR operation.
  ```java
  int result = 5 | 3; // result = 7
  ```


- XOR (^):

Performs a bitwise XOR operation.
  ```java
  int result = 5 ^ 3; // result = 6
  ```


- Complement (~):

Inverts all the bits.
  ```java
  int result = ~5; // result = -6
  ```

- Left Shift (<<):

Shifts bits to the left, filling with zeros.
  ```java
  int result = 5 << 1; // result = 10
  ```

- Right Shift (>>):

Shifts bits to the right, filling with the sign bit.
  ```java
  int result = 5 >> 1; // result = 2
  ```

- Unsigned Right Shift (>>>):

Shifts bits to the right, filling with zeros.
  ```java
  int result = 5 >>> 1; // result = 2
  ```


 4.6 Conditional Operator


The conditional operator, also known as the ternary operator, is a shorthand way of writing an `if-else` statement.

- Syntax:

 `condition ? expression1 : expression2`
  ```java
  int result = (5 > 3) ? 10 : 20; // result = 10
  ```
https://techyajay12.blogspot.com/2024/07/chapter-4-operators-in-java-language.html



4.7 instanceof Operator


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

- Syntax:

`object instanceof class`
  ```java
  String str = "Hello";
  boolean result = str instanceof String; // true
  ```


 Conclusion

Operators are fundamental in Java programming, providing the means to perform a variety of operations on variables and values. Mastering these operators is essential for writing concise and efficient code. This chapter covered the essential operators, laying a strong foundation for more advanced Java programming concepts.




 

Comments