Module 6 - Java Operators
Module 6 - Java Operators
6 Chapter
JAVA OPERATORS
JAVA OPERATORS
Java provides a rich set of operations environment. Java operators can be divided into the following
categories:
1. Arithmetic Operators
2. Increment & Decrement Operators
3. Relational Operators
4. Logical Operators
5. Bitwise Operators
6. Assignment Operators
7. Conditional Operators
1. Arithmetic Operators
Arithmetic operators are used in mathematical expression in the same way that are used in algebra.
SAMPLE INPUT 1
Here's a sample program in the usage of these operators:
System.out.println("Variable values...");
System.out.println(" i = " + i);
System.out.println(" j = " + j);
System.out.println(" x = " + x);
System.out.println(" y = " + y);
//adding numbers
System.out.println("Adding...");
System.out.println(" i + j = " + (i + j));
System.out.println(" x + y = " + (x + y));
//subtracting numbers
System.out.println("Subtracting...");
System.out.println(" i - j = " + (i - j));
System.out.println(" x - y = " + (x - y));
//multiplying numbers
System.out.println("Multiplying...");
System.out.println(" i * j = " + (i * j));
System.out.println(" x * y = " + (x * y));
//dividing numbers
System.out.println("Dividing...");
System.out.println(" i / j = " + (i / j));
System.out.println(" x / y = " + (x / y));
//mixing types
System.out.println("Mixing types...");
System.out.println(" j + y = " + (j + y));
System.out.println(" i * x = " + (i * x));
SAMPLE OUTPUT 1
Here is the output of the program,
Variable values...
i = 37
j = 42
x = 27.475
y = 7.22
Adding...
i + j = 79
x + y = 34.695
Subtracting...
i - j = -5
x - y = 20.255
Multiplying...
i * j = 1554
x * y = 198.37
Dividing...
i / j = 0
x / y = 3.8054
Mixing types...
j + y = 49.22
i * x = 1016.58
The increment and decrement operators can be placed before or after an operand.
When used before an operand, it causes the variable to be incremented or decremented by 1, and
then the new value is used in the expression in which it appears.
SAMPLE INPUT 2
Here's a sample program in the usage of these operators:
int i = 10;
int j = 3;
int k = 0;
int z;
k = ++j + i;
z = j++ + i;
System.out.println(“k”+(k));
System.out.println(“z”+(z));
}
}
SAMPLE OUTPUT 2
Result: k = 14
Z = 13
3. Relational Operators
Relational operators compare two values and determines the relationship between those values.
The output of evaluation are the Boolean values true or false.
SAMPLE INPUT 3
Here's a sample program that uses relational operators,
System.out.println("Variable values...");
System.out.println(" i = " + i);
System.out.println(" j = " + j);
System.out.println(" k = " + k);
//greater than
System.out.println("Greater than...");
System.out.println(" i > j = " + (i > j));
System.out.println(" j > i = " + (j > i));
System.out.println(" k > j = " + (k > j));
//less than
System.out.println("Less than...");
System.out.println(" i < j = " + (i < j));
System.out.println(" j < i = " + (j < i));
System.out.println(" k < j = " + (k < j));
//equal to
System.out.println("Equal to...");
//not equal to
System.out.println("Not equal to...");
System.out.println(" i != j = " + (i != j));
System.out.println(" k != j = " + (k != j));
SAMPLE OUTPUT 3
Here's the output from this program:
Variable values...
i = 37
j = 42
k = 42
Greater than...
i > j = false
j > i = true
k > j = false
Less than...
i < j = true
j < i = false
k < j = false
Equal to...
i == j = false
k == j = true
4. Logical Operators
Logical operators have one or two Boolean operands that yield a Boolean result.
SAMPLE INPUT 4
Here's a sample source code that uses logical AND,
int i = 0;
int j = 10;
boolean test= false;
}
}
SAMPLE OUTPUT 4
Result: 0
10
false
2. | | (logical OR)
SAMPLE INPUT 5
Here's a sample source code that uses logical OR,
int i = 0;
int j = 10;
boolean test= false;
}
}
SAMPLE OUTPUT 5
Result: 0
10
true
3. ! (logical NOT)
SAMPLE INPUT 6
Here's a sample source code that uses the logical NOT operator,
System.out.println(!val1);
System.out.println(!val2);
SAMPLE OUTPUT 6
The output of the program is,
false
true
5. Bitwise Operators
Operator Description
& (Ampersand) Bitwise AND
| (Pipe) Bitwise OR
^ (Caret) Bitwise exclusive OR
<< Left shift
>> Right shift
0 0 0 0 0
0 1 0 0 1
1 0 0 0 1
1 1 1 1 0