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:
Binary Arithmetic Operators in Java
Relational 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 following series of images presents a series of relational operators that are commonly used, which compare two numbers and return a Boolean (true or false) result:
Relational Operators
Relational operators are used to determine the relationship, or relative ordering, between two operands. These operators frequently use two symbols. For example, greater than or equal to is expressed using the >= operator. The ordering of the symbols is important. Using => is not legal.
Convert the following uploaded image into a HTML table consisting of 6 rows and 3 columns.
Equality Operator
The equality operator consists of two equals signs and when evaluated will return either a true or a false value. The assignment operator uses a single equal sign and will modify its left operand. To illustrate these operators, consider the following example. If a value of a rate variable equals 100, we could assume that an error is present. To reflect this error condition we could assign a true value to the errorPresent variable. This can be performed using both the assignment and the equality operators.
int rate;
rate = 100;
boolean errorPresent = rate==100;
System.out.println(errorPresent);
When the preceding code snippet is executed we get the following output:
true
The logical expression, rate==100, compares the value stored in rate to the integer literal 100. If they are equal, which is the case here, the expression returns true. The true value is then assigned to errorPresent. If the value stored in rate had not been 100, then the expression will return a value of false.