Aside from Java commands, Java operators are one of the most important tools when using the pro­gram­ming language. With a well-struc­tured overview, you will always have all the important Java operators to hand when you need them.

What are Java operators?

A Java operator can be used to perform op­er­a­tions on a set of values. There are a number of different Java operators, which are divided into unary, binary and ternary operators. Unary means that the Java operator has one digit, binary describes a two-digit operator, and the ternary con­di­tion­al operator has three digits.

An example of a unary Java operator is negation, which is expressed by ‘!example’. Sub­trac­tion is an example of a binary Java operator because it requires two operands (a - b). The only ternary Java operator is the con­di­tion­al operator, which works according to the if-then-else method:

( <boolean expression=""> ) ? OutputValueTrue : OutputValueFalse;</boolean>

In the following we will introduce you to the different Java operators.

Arith­met­ic Java operators

The rules of arith­met­ic Java operators include the basic math­em­at­ic­al op­er­a­tions of addition, sub­trac­tion, mul­ti­plic­a­tion and division.

Java operator De­scrip­tion
+ In addition, the values of two operands are added together.
- In sub­trac­tion, the value of the second operand is sub­trac­ted from the value of the first operand.
* In mul­ti­plic­a­tion, two operands are mul­ti­plied together.
/ In division, the first operand is divided by the value of the second operand.
% The modulo operand cal­cu­lates the remaining value of a division.
+ The plus can also be used as a positive sign. However, this Java operator is not needed in most cases.
- The minus can also be used as a negative sign.

In­cre­ment­ing and decre­ment­ing

In­cre­ment­ing or decre­ment­ing is required regularly in pro­gram­ming. For this purpose there are Java operators for in­cre­ment­ing (counting up) and decre­ment­ing (counting down).

Java operator De­scrip­tion
++ In­cre­ment­ing increases the value of a numeric variable by +1.
-- Decre­ment­ing decreases the value of a numeric variable by -1.

When in­cre­ment­ing and decre­ment­ing, a dis­tinc­tion is made between post-increment and post-decrement and pre-increment and pre-decrement. While the math­em­at­ic­al char­ac­ters are placed after the variable in the first variant, they are listed first in the other variant. For example: ‘a++’ compared to ‘++a’. This makes a dif­fer­ence for the different steps. Here is the com­par­is­on between the two ap­proaches:

  • a++: Post-increment causes the value of ‘a’ to be used first and ‘a’ to be in­cre­men­ted by ‘1’ only af­ter­wards.
  • ++a: Pre-increment causes the value of ‘a’ to be increased by ‘1’ first and only used af­ter­wards.

Java operators for com­par­is­ons

Com­par­is­ons can also be made with the help of Java operators. For this purpose, two operands are compared with each other and the result is output as a Boolean value.

Java operator De­scrip­tion
< For ‘less than’, the value is given as ‘true’ if a is less than b.
> For ‘greater than’, the value is given as ‘true’ if a is greater than b.
<= For ‘less than or equal to’, the value is given as ‘true’, provided that a is less than or equal to b.
>= For ‘greater than or equal to’, the value is specified as ‘true’, provided that a is greater than or equal to b.
== For ‘equal to’ the value is given as ‘true’ if a and b are equal.
!= For ‘not equal to’ the value is given as ‘true’ if the size of a is not equal to the size of b.
Note

The term ‘Boolean’ goes back to the English math­em­atician and philo­soph­er George Boole and is also called ‘truth value’. This element of Boolean algebra only knows the states ‘true’ or ‘untrue’. As a switching variable, the Boolean value is used in pro­gram­ming and digital tech­no­logy.

Ad­di­tion­al Java Boolean operators

In addition to the Java operators listed above, there are other variants that are based on the Boolean principle. These include the following Java operators:

Java operator De­scrip­tion
! The negation inverts the truth of an operand from ‘true’ to ‘false’ or vice versa.
&& The Java operator && (And) returns ‘true’ only if both operands a and b are true.
|| || (Or) returns ‘true’ if at least one of the two operands a and b is true.
^ Exor returns ‘true’ only if exactly one of the two operands a and b is true.

Bitwise Java operators

Bitwise Java operators are used to ma­nip­u­late in­di­vidu­al bits of numbers. The function is similar to the other Boolean operators. However, with the bitwise operators, each bit of the first operand is compared with the cor­res­pond­ing bit of the second operand.

Java operator De­scrip­tion
~ All bits of an operand are inverted.
& With bitwise And a ‘1’ is produced, provided that both operands are also ‘1’. If this is not the case, a ‘0’ is output.
| The bitwise OR produces a ‘1’ if one of the two operands a and b is also ‘1’.
^ The bitwise exclusive OR produces a ‘0’ if both operands have the same value. If this does not apply, a ‘1’ is produced.
>> With this Java operator, all bits of the operand are shifted one place to the right.
>>> Shifts the bits of operand a by b positions to the right. Padding is done with zeros.
<< Shifts the bits of operand a by b positions to the left. Padding is performed with ‘0’.

The com­bin­a­tion tool for Java operators, as­sign­ment operators

As­sign­ment operators are an important tool for assigning values to variables. In each case, an ex­pres­sion is evaluated and applied to the front variable. The simplest variant is a = b, where the value of b is assigned to a. The as­sign­ment operators often con­trib­ute to shorter lines and can be combined with arith­met­ic, logical and bitwise Java operators.

Java operator De­scrip­tion
= The simple as­sign­ment assigns the value of b to the operand a.
+= a is assigned the value of a + b.
-= The operand a is assigned the value of a - b.
*= Mul­ti­plic­a­tion as­sign­ment causes a to receive the result of a * b.
/= Here, a receives the result of dividing a by b.
%= With this Java operator, a is assigned the value of a % b.
&= The operand a gets the value of a & b.
|= With |= a gets the value of a | b and a | b is returned as return value.
^= a receives the result of a ^ b as new value.
<<= The result of a << b is assigned to the operand a as a new value.
>>= a gets the value of a >> b.
>>>= a is assigned the result of a >>> b.
Go to Main Menu