Java Programming - Operators
Operators
2 november
Operators are used to perform operations on variables and values.
Arithmetic Operators
2 november
that they are used in algebra.
Assignment Operator
2 november
Language.
var = expression;
int x, y, z;
x = y = z = 100; // set x, y, and z to 100
Bitwise Operators
2 november
long, int, short, char, and byte. These operators act upon the individual bits of
their operands.
Relational Operators
2 november
other
Boolean Logical Operators
2 november
resultant boolean value.
The ? Operator
Output
2 november
types of if-then-else statements.
Syntax:
expression1 ? expression2 : expression3 ;
Example:
x = a < 1 ? -1 : 2;
Operator Precedence
2 november
Using Parentheses
2 november
a >> b + 3
Arithmetic Operators
The Modulus Operators
Output
Arithmetic Compound Assignment Operators
Output
operation with an assignment.
a = a + 4; ----> a += 4;
a = 6
b = 8
c = 3
Increment and Decrement Operators
Output
operator decreases its operand by one.
x = x + 1; ----> x++;
x = x - 1; -----> x--;
y = ++x; ----> x = x + 1;
y = x;
y = x++; -----> y = x;
x = x + 1;
The Bitwise Logical Operators
Output
The Left Shift Operators
Output
number of times. It has this general form:
value << num
The Right Shift Operators
Output
Output
Signed right shift operator, >>, shifts all of the bits in a value to the right a specified number of times. Its general form is shown here:
value >> num
-2
2
>>> (Unsigned right shift) In Java, the operator ‘>>>’ is unsigned right shift operator. It always fills 0 irrespective of the sign of the number.
7
3
1
Bitwise Operator Compound Assignments
Output
a = 3
b = 1
c = 6
Relational Operators
The relational operators determine the relationship that one operand has to the other.
class Main {
public static void main(String[] args) {
int a = 7, b = 11;
System.out.println("a is " + a + " and b is " + b);
// == operator
System.out.println(a == b); // false
// != operator
System.out.println(a != b); // true
// > operator
System.out.println(a > b); // false
// < operator
System.out.println(a < b); // true
// >= operator
System.out.println(a >= b); // false
// <= operator
System.out.println(a <= b); // true
}}
Boolean Logical Operators
Output
The relational operators determine the relationship that one operand has to the other.
Thank You