Numeric operators are used to manipulate numbers, and are divided across three different types: unary, binary, and relational.
Unary operators operate on a single number, with the two most commonly used unary operators being ++ and --.
These operators are used to increment or decrement a number by 1.
Unary operators in an expression can be used as either prefix or postfix operators.
A prefix operator is applied before the expression is evaluated, while a postfix operator is applied after the expression is evaluated.
Following are the commonly used binary operators:
Relational operators are used to check one condition. You can use these operators to determine whether a primitive value is equal to another value or whether it is less than or greater than the other value. These relational operators can be divided into two categories:
- Comparing greater (>, >=) and lesser values (<, <=)
- Comparing values for equality (==) and nonequality (!=)
The operators <, <=, >, and >= work with all types of numbers, both integers (including char) and floating point, that can be added and subtracted.
Examine the following code:
int i1 = 10;
int i2 = 20;
System.out.println(i1 >= i2);
false
long long1 = 10;
long long2 = 20;
System.out.println(long1 <= long2);
true
The second category of operators is covered in the following section.