Certainly! In Java, you can use various operators and expressions to perform arithmetic operations, assignments, relational comparisons, logical operations, and increment/decrement operations. Here's an overview of these operators and expressions:
1.Arithmetic Operators:
' + ' (Addition): Adds two operands.
' - ' (Subtraction): Subtracts the right operand from the left operand.
' * ' (Multiplication): Multiplies two operands.
' / ' (Division): Divides the left operand by the right operand.
' % ' (Modulus): Returns the remainder of the division of the left operand by the right operand.
Example:
int a = 10, b = 5;
int sum = a + b;
int difference = a - b;
int product = a * b;
int quotient = a / b;
int remainder = a % b;
2.Assignment Operators:
= (Assignment): Assigns the value of the right operand to the left operand.
+= (Addition Assignment): Adds the right operand to the left operand and assigns the result to the left operand.
-= (Subtraction Assignment): Subtracts the right operand from the left operand and assigns the result to the left operand.
*= (Multiplication Assignment): Multiplies the left operand by the right operand and assigns the result to the left operand.
/= (Division Assignment): Divides the left operand by the right operand and assigns the result to the left operand.
%= (Modulus Assignment): Computes the modulus of the left operand and the right operand and assigns the result to the left operand.
Example:
int x = 10; x += 5; // x is now 15
x -= 3; // x is now 12
x *= 2; // x is now 24
x /= 4; // x is now 6
x %= 3; // x is now 0
3.Relational Operators:
== (Equal to): Checks if two operands are equal.
!= (Not equal to): Checks if two operands are not equal.
< (Less than): Checks if the left operand is less than the right operand.
> (Greater than): Checks if the left operand is greater than the right operand.
<= (Less than or equal to): Checks if the left operand is less than or equal to the right operand.
>= (Greater than or equal to): Checks if the left operand is greater than or equal to the right operand.
Example:
int num1 = 10, num2 = 20;
boolean isEqual = (num1 == num2); // false
boolean isNotEqual = (num1 != num2); // true
boolean isLessThan = (num1 < num2); // true
boolean isGreaterThan = (num1 > num2); // false
4.Logical Operators:
&& (Logical AND): Returns true if both operands are true.
|| (Logical OR): Returns true if at least one operand is true.
! (Logical NOT): Returns the opposite boolean value of the operand.
Example:
boolean isTrue = true, isFalse = false;
boolean result1 = isTrue && isFalse; // false
boolean result2 = isTrue || isFalse; // true
boolean result3 = !isTrue; // false
5.Increment and Decrement Operators:
++ (Increment): Increases the value of the operand by 1.
-- (Decrement): Decreases the value of the operand by 1.
Example:
int count = 5;
count++; // count is now 6
count--; // count is now 5
These operators and expressions are fundamental in Java for performing various operations on variables and data. They are essential for building complex logic in Java programs.
Example-
JAVA Arithmetic, Assignment, Relational, Logical, Increment / Decrement operators and expressions. write a program
public class OperatorDemo {
public static void main(String[] args) {
// Arithmetic operators
int num1 = 10;
int num2 = 5;
int sum = num1 + num2;
int difference = num1 - num2;
int product = num1 * num2;
int quotient = num1 / num2;
int remainder = num1 % num2;
System.out.println("Arithmetic Operators:");
System.out.println("Sum: " + sum);
System.out.println("Difference: " + difference);
System.out.println("Product: " + product);
System.out.println("Quotient: " + quotient);
System.out.println("Remainder: " + remainder);
// Assignment operators
int x = 5;
x += 3; // Equivalent to x = x + 3
System.out.println("\nAssignment Operators:");
System.out.println("x after assignment: " + x);
// Relational operators
int a = 10; int b = 20;
boolean isEqual = (a == b);
boolean isNotEqual = (a != b);
boolean isGreater = (a > b);
boolean isLess = (a < b);
System.out.println("\nRelational Operators:");
System.out.println("Is equal: " + isEqual);
System.out.println("Is not equal: " + isNotEqual);
System.out.println("Is greater: " + isGreater);
System.out.println("Is less: " + isLess);
// Logical operators
boolean p = true;
boolean q = false;
boolean logicalAnd = p && q;
boolean logicalOr = p || q;
boolean logicalNot = !p;
System.out.println("\nLogical Operators:");
System.out.println("Logical AND: " + logicalAnd);
System.out.println("Logical OR: " + logicalOr);
System.out.println("Logical NOT: " + logicalNot);
// Increment and Decrement operators
int count = 5; count++; // Increment by 1
int anotherCount = 10;
anotherCount--; // Decrement by 1
System.out.println("\nIncrement/Decrement Operators:");
System.out.println("count after increment: " + count);
System.out.println("anotherCount after decrement: " + anotherCount);
}
}
Output
Arithmetic Operators:
Sum: 15
Difference: 5
Product: 50
Quotient: 2
Remainder: 0
Assignment Operators:
x after assignment: 8
Relational Operators:
Is equal: false
Is not equal: true
Is greater: false
Is less: true
Logical Operators:
Logical AND: false
Logical OR: true
Logical NOT: false
Increment/Decrement Operators:
count after increment: 6
anotherCount after decrement: 9