Java Operators Tutorial.
Java Operators:
In Java their are serveral types of numerator
- Arithmetic operators
- Comparison operators
- Increment and decrement operators
- Logical operators
- Bitwise and Shift operators
1. Java Arithmetic operators
Java contains five simple aritmethic operators, the below table display each numerator and how they are used in java.
|
numerator |
Use |
Description |
|
+ |
num1
+ num2 |
Adds num1 and num2 |
|
- |
num1
- num2 |
Subtracts num2
from num1 |
|
* |
num1
* num2 |
Multiplies num1
by num2 |
|
/ |
num1
/ num2 |
Divides num1 by
num2 |
|
% |
num1
% num2 |
computes the
remainder of dividing num1 by num2 |
|
+ |
+num |
Promotes + to
int if its a byte,short or char |
|
- |
-num |
Arithmetically
negates num |
|
++ |
num++ |
Increments num
by 1; evaluates to value before incrementing |
|
++ |
++num |
Increments num
by 1; evaluates to value after incrementing |
|
-- |
--num |
Decrements num
by 1; evaluates to value before decrementing |
|
-- |
num-- |
Decrements num
by 1; evaluates to value after decrementing |
Examples:
public class sampleOperators{
public static void main(String[] args) {
int numA = 5;
int numB = 3;
double numC = 10.5;
double numD = 20.5;
//adding numbers
System.out.println(" numA + numB = " + (numA + numB));
//subtracting numbers
System.out.println(" numA - numB = " + (numA - numB));
//multiplying numbers
System.out.println(" numC* numD = " + (numC * numD));
//dividing numbers
System.out.println(" numD / numC = " + (numD / numC));
//computing the remainder resulting Modulus
System.out.println(" 7 % 3 = " + (7 % 3));
}
}
The output from the above examples would be:
//adding numbers
8
//subtracting numbers
2
//multiplying numbers
215.25
//dividing numbers
1.95
//computing the remainder resulting Modulus
1 - as it the remainder after deviding 7 by 2.