Java Fundamentals  «Prev  Next»
Lesson 8Using numeric operators
ObjectiveUse Java's commonly-used Numeric Operators

Numeric Operators in Java Programming

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
Addition and string concatenation
1) Addition: Adds two numbers or concatenates two strings.

Subtraction
2) Subtraction: Subtracts a number from another number.

Multiplication
3) Multiplication: Multiples two numbers.

Division
4) Division: Divides a number by another number.

Modulus
5) Modulus: Determines the remainder of a division


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:
  1. Comparing greater (>, >=) and lesser values (<, <=)
  2. 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:


 Less-than: Compares two values and returns true if the first is less than the second.
1)Less-than: Compares two values and returns true if the first is less than the second.

 Greater-than: Compares two values and returns true if the first is greater than the second
2)Greater-than: Compares two values and returns true if the first is greater than the second

 Less-than-or-equal-to: Compares two values and returns true if the first is less than or equal to the second.
3)Less-than-or-equal-to: Compares two values and returns true if the first is less than or equal to the second.

 Greater-than-or-equal-to: Compares two values and returns true if the first is greater than or equal to the second.
4)Greater-than-or-equal-to: Compares two values and returns true if the first is greater than or equal to the second.

 Equal-to: Compares two values and returns true if they are equal
5)Equal-to: Compares two values and returns true if they are equal

 Not-equal-to: Compares two values and returns true if they are not equal.
6)Not-equal-to: Compares two values and returns true if they are not equal.


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.
Java Relational Operators
The relational operators are listed in the table above:


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.

SEMrush Software