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

The following slide show presents a series of relational operators that are commonly used, which compare two numbers and return a Boolean (true or false) result:
1) Beautiful 1 2) Beautiful 2 3) Beautiful 3 4) Beautiful 4 5) Beautiful 5 6) Beautiful 6

Program 1 Program 2 Program 3 Program 4 Program 5 Program 6
  1. Less-than: Compares two values and returns true if the first is less than the second.
  2. Greater-than: Compares two values and returns true if the first is greater than 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.
  4. Greater-than-or-equal-to: Compares two values and returns true if the first is greater than or equal to the second.
  5. Equal-to: Compares two values and returns true if they are equal
  6. Not-equal-to: Compares two values and returns true if they are not equal.

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 second category of operators is covered in the following section.