Operators in Java - AsgarTech

Operators in Java - AsgarTech 

In Java, an operator is a symbol or set of symbols that performs an operation on one or more operands. Operators are used to manipulate values and variables in expressions and statements to produce results.


Java has several types of operators


Arithmetic Operators:

Arithmetic operators are used to perform basic mathematical operations such as addition, subtraction, multiplication, and division. They include the following:


Addition (+)

Subtraction (-)

Multiplication (*)

Division (/)

Modulo (%)


Assignment Operators:

Assignment operators are used to assign a value to a variable. They include the following:


  1. Assignment (=)
  2. Addition assignment (+=)
  3. Subtraction assignment (-=)
  4. Multiplication assignment (*=)
  5. Division assignment (/=)
  6. Modulo assignment (%=)
  7. Bitwise AND assignment (&=)
  8. Bitwise OR assignment (|=)
  9. Bitwise XOR assignment (^=)
  10. Left shift assignment (<<=)
  11. Right shift assignment (>>=)
  12. Unsigned right shift assignment (>>>=)


Comparison Operators:

Comparison operators are used to compare two values or variables. They include the following:


  1. Equal to (==)
  2. Not equal to (!=)
  3. Greater than (>)
  4. Greater than or equal to (>=)
  5. Less than (<)
  6. Less than or equal to (<=)


Logical Operators:

Logical operators are used to combine two or more conditions to produce a boolean result. They include the following:


  1. Logical AND (&&)
  2. Logical OR (||)
  3. Logical NOT (!)

Bitwise Operators:


Bitwise operators are used to perform operations on the binary representation of values. They include the following:


  1. Bitwise AND (&)
  2. Bitwise OR (|)
  3. Bitwise XOR (^)
  4. Bitwise complement (~)
  5. Left shift (<<)
  6. Right shift (>>)
  7. Unsigned right shift (>>>)


Conditional Operators:

Conditional operators are used to evaluate a condition and return one of two values depending on the result. They include the following:


  1. Ternary operator (?:)


These are the main types of operators in Java. Understanding how to use them and how they work is essential for programming in Java.

Here are some examples of operators in Java


Arithmetic Operators:

int x = 5;
int y = 3;

int sum = x + y; // addition
int difference = x - y; // subtraction
int product = x * y; // multiplication
int quotient = x / y; // division
int remainder = x % y; // modulo


Assignment Operators:


int x = 5;
x += 3; // equivalent to x = x + 3;
x -= 2; // equivalent to x = x - 2;
x *= 4; // equivalent to x = x * 4;
x /= 2; // equivalent to x = x / 2;
x %= 3; // equivalent to x = x % 3;


Comparison Operators:

int x = 5;
int y = 3;

boolean isEqual = x == y; // equal to
boolean isNotEqual = x != y; // not equal to
boolean isGreater = x > y; // greater than
boolean isGreaterOrEqual = x >= y; // greater than or equal to
boolean isLess = x < y; // less than
boolean isLessOrEqual = x <= y; // less than or equal to


Logical Operators:

int x = 5;
int y = 3;
int z = 7;

boolean condition1 = x > y && x < z; // logical AND
boolean condition2 = x > y || x > z; // logical OR
boolean condition3 = !(x == y); // logical NOT

Bitwise Operators:

int x = 5;
int y = 3;

int bitwiseAnd = x & y; // bitwise AND
int bitwiseOr = x | y; // bitwise OR
int bitwiseXor = x ^ y; // bitwise XOR
int bitwiseComplement = ~x; // bitwise complement
int leftShift = x << 2; // left shift by 2 bits
int rightShift = x >> 1; // right shift by 1 bit
int unsignedRightShift = x >>> 1; // unsigned right shift by 1 bit

Conditional Operator:

int x = 5;
int y = 3;

int max = (x > y) ? x : y; // ternary operator to find the maximum value

check these all Courses blog👇👇







thanks please share with your friends👀





No comments: