operator in java
operator in java
Java provides rich set of operators, mainly divided into four groups viz. arithme c, bitwise, rela onal
and logical. These operators are discussed here.
Arithme c Operators
Arithme c operators are used in mathema cal expressions in the same way that they are used in
algebra. The following table lists the arithme c operators:
Operator Meaning
+ Addi on
- Subtrac on
* Mul plica on
/ Division
% Modulus
++ Increment
-- Decrement
+= Addi on assignment
-= Subtrac on assignment
/= Division assignment
%= Modulus assignment
The operands of the arithme c operators must be of a numeric type. You cannot use them on
Boolean types, but you can use them on char types, since the char type in Java is a subset of int.
Note down following few points about various operators:
Basic arithme c operators like +, -, * and / behave as expected for numeric data.
The – symbol can be used as unary operator to negate a variable.
If / is operated on two integer operands, then we will get only integral part of the result by
trunca ng the frac onal part.
// Subtraction
int difference = a - b;
System.out.println("a - b = " + difference);
// Multiplication
int product = a * b;
System.out.println("a * b = " + product);
// Division
int quotient = a / b;
System.out.println("a / b = " + quotient);
}
}
This program declares two integer variables a and b and performs arithmetic operations on them
using the following operators:
+ for addition
- for subtraction
* for multiplication
/ for division
MODULUS OPERATOR
The % operator returns the remainder a er division. It can be applied on integer and floa ng-
point types. For example,
int x=57;
double y= 32.8;
var op = expression
is same as a=a+2;
a += 3; // equivalent to a = a + 3;
b -= 4; // equivalent to b = b - 4;
c *= 2; // equivalent to c = c * 2;
d /= 5; // equivalent to d = d / 5;
e %= 5; // equivalent to e = e % 5;
a++; a=a+1;
b--; b=b-1;
class IncrementDecrement {
public sta c void main(String[] args) {
int x = 10;
System.out.println("Ini al value of x: " + x);
// Pre-increment operator
++x;
System.out.println("Value of x a er pre-increment: " + x);
// Post-increment operator
x++;
System.out.println("Value of x a er post-increment: " + x);
// Pre-decrement operator
--x;
System.out.println("Value of x a er pre-decrement: " + x);
// Post-decrement operator
x--;
System.out.println("Value of x a er post-decrement: " + x);
}
}
OUTPUT
Ini al value of x: 10
Value of x a er pre-increment: 11
Value of x a er post-increment: 12
Value of x a er pre-decrement: 11
Value of x a er post-decrement: 10
NOTE :-That in C/C++, the % operator cannot be used on float or double and should be used only
on integer variable.
Operator Meaning
== Equal to (or comparison)
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
The outcome of these opera ons is a boolean value. Any type in Java, including integers, floa ng-
point numbers, characters, and Booleans can be compared using the equality test, ==, and the
inequality test, !=. Only numeric types can be compared using the ordering operators. That is, only
integer, floa ng-point, and character operands may be compared to see which is greater or less than
the other. For example, the following code fragment is perfectly valid:
int a = 4;
int b = 1;
boolean c = a < b;
//Example program
class RelationalOperators {
public static void main(String[] args) {
int num1 = 5, num2 = 10;
System.out.println("num1 > num2: " + (num1 > num2));
System.out.println("num1 < num2: " + (num1 < num2));
System.out.println("num1 >= num2: " + (num1 >= num2));
System.out.println("num1 <= num2: " + (num1 <= num2));
System.out.println("num1 == num2: " + (num1 == num2));
System.out.println("num1 != num2: " + (num1 != num2));
}
}
class BoolLogic
{
int h= 12;
int i= 9;
int j = 0b1010;
boolean a = true;
boolean b = false;
boolean c = a | b;
boolean d = a & b;
boolean e = a ^ b;
boolean g = !a;
h &= 5; // equivalent to h = h & 5;
i |= 6; // equivalent to i = i | 6;
System.out.println("New a is "+a);
System.out.println ("h: " + h);
System.out.println ("i: " + i);
System.out.println ("j: " + j);
}
a = true
b = false
a|b = true
a&b = false
a^b = true
!a&b|a&!b = true
!a = false
New a is false
h: 4
i: 15
j: 6
In this program, we have variables "a" and "b". We use the "&&" operator to test if "a" is more than five and "b"
is less than 10. If each situa on is true, then we print "Both situa ons are true." Otherwise, we print "At least
one condi on is false."
var = expression;
Here, the type of var must be compa ble with the type of expression. It allows you to create a chain
of assignments. For example, consider this fragment:
int x, y, z;
This fragment sets the variables x, y, and z to 100 using a single statement. This works because the =
is an operator that yields the value of the right-hand expression. Thus, the value of z = 100 is 100,
which is then assigned to y, which in turn is assigned to x. Using a “chain of assignment” is an easy
way to set a group of variables to a common value.
The ? : Operator
Java supports ternary operator which some mes can be used as an alterna ve for if-then-else
statement.
Here, expression1 is evaluated first and it must return Boolean type. If it results true, then value of
expression2 is executed else expression3 is executed
//Example program
Int feb=28;
String res;
Output:
Operator Precedence
Following table describes the precedence of operators. Though parenthesis, square brackets etc. are
separators, they do behave like operators in expressions. Operators at same precedence level will be
evaluated from le to right, whichever comes first.
Using Parentheses
Parentheses always make the expression within them to execute first. This is necessary some mes.
For example,
a= b – c * d;
Here, c and d are mul plied first and then the result is subtracted from b. If we want subtrac on first,
we should use parenthesis like
a= (b-c)*d;
Some mes, parenthesis is useful for clarifying the meaning of an expression and for making readers
to understand the code. For example,
In such situa ons, though parenthesis seems to be redundant, it existence will not reduce the