Operators are symbols used to perform operations on variable and values. Hear are some common use of operators
Arithmetic Operators: Used to perform arithmetic operations like
1) addition //
2) subtraction
3) multiplication
4) division
5) modulus
//Example
int a = 10;
int b = 5;
int sum = a + b; // Addition sum = 15
int difference = a - b; // Subtraction >sum = 5
int product = a * b; // Multiplication>>sum = 50
int quotient = a / b; // Division >> sum = 2
int remainder = a % b; // Modulus >> sum = 10
The addition operator in Java (+) is used to add two operands(on which operation performs) together. It can be used with numeric data types like int, double, float, long, etc., as well as with String objects for concatenation.
Here's how it works:
Numeric Addition:
When the + operator is used with numeric operands (e.g., integers, floating-point numbers), it performs addition, returning the sum of the operands.
int a = 5;
int b = 3;
int sum = a + b; // sum will be 8
String Concatenation:
When one or both operands are of type String, the + operator concatenates the operands into a single String.
String str1 = "Hello";
String str2 = "World";
String result = str1 + str2; // result will be "HelloWorld"
Along with substraction (-) it has one more use
Unary Negation:
- When the
-
operator is used as a unary operator, before a single operand, it negates the value of that operand.
int num = 10; int negation = -num; // negation will be -10
- When the
Mixing Numeric and String Operands:
If one operand is a String, the other operand is converted to a String and then concatenated.
int num = 10;
String message = "The value of num is: " + num; // message will be "The value of num is: 10"
the addition operator in Java performs either arithmetic addition or string concatenation based on the types of its operands. If both operands are numeric, it performs addition; if one or both operands are strings, it concatenates them.
Use of modulo(%) operator in java
>>the modulo operator gives the remainder by dividing left to righ t value;
i.e
int a = 10;
int b = 3;
int remainder = a % b; // remainder will be 1
Assignment Operators: Used to assign values to variables.
//Example
int x = 10;
x += 5; // Equivalent to: x = x + 5;
Comparison Operators: Used to compare two values and return True or False.
int num1 = 10;
int num2 = 20;
boolean isEqual = (num1 == num2); // Equal to if both are equal returns true
boolean isNotEqual = (num1 != num2); // Not equal to if both are not equal return false
boolean isGreater = (num1 > num2); // if num1 greater than num2 return true
boolean isLess = (num1 < num2); // if num1 less than num2 return true
Logical Operators: Used to perform logical operations using Logical AND , Logical OR , Logical NOT.
boolean isTrue = true;
boolean isFalse = false;
boolean resultAnd = isTrue && isFalse; // Logical AND in this case it return false
boolean resultOr = isTrue || isFalse; // Logical OR in this case it return true
boolean resultNot = !isTrue; // Logical NOT in this case it return true be it invert the value of isTrue ;
Bitwise Operators: Used to perform bitwise operations.
int a = 5; // 101 in binary
int b = 3; // 011 in binary
int bitwiseAnd = a & b; // Bitwise AND (001)
int bitwiseOr = a | b; // Bitwise OR (111)
int bitwiseXor = a ^ b; // Bitwise XOR (110)
int bitwiseComplement = ~a; // Bitwise Complement (11111010)
int leftShift = a << 1; // Left shift by 1 (1010)
int rightShift = a >> 1; // Right shift by 1 (10)
Conditional Operator (Ternary Operator): Used to make decisions based on a condition.
This is for advance programmers.
int x = 10; int y = 5; int max = (x > y) ? x : y;
// If x > y, max = x; otherwise Y =max, max = y;
This is for advance programmers.
Instanceof Operator: Used to check whether an object is an instance of a particular class or interface.
Object obj = new String("Hello");
boolean isString = (obj instanceof String); // true if obj is an instance of String