Java Operators

Aside from Java commands, Java operators are one of the most important tools when using the programming language. With a well-structured 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 operations 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 conditional operator has three digits.

An example of a unary Java operator is negation, which is expressed by “!example”. Subtraction is an example of a binary Java operator because it requires two operands (a - b). The only ternary Java operator is the conditional 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.

Arithmetic Java operators

The rules of arithmetic Java operators include the basic mathematical operations of addition, subtraction, multiplication and division.

Java operator

Description

+

In addition, the values of two operands are added together.

-

In subtraction, the value of the second operand is subtracted from the value of the first operand.

*

In multiplication, two operands are multiplied together.

/

In division, the first operand is divided by the value of the second operand.

%

The modulo operand calculates 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.

Incrementing and decrementing

Incrementing or decrementing is required regularly in programming. For this purpose there are Java operators for incrementing (counting up) and decrementing (counting down).

Java operator

Description

++

Incrementing increases the value of a numeric variable by +1.

--

Decrementing decreases the value of a numeric variable by -1.

When incrementing and decrementing, a distinction is made between post-increment and post-decrement and pre-increment and pre-decrement. While the mathematical characters 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 difference for the different steps. Here is the comparison between the two approaches:

  • a++: Post-increment causes the value of “a” to be used first and “a” to be incremented by “1” only afterwards.
  • ++a: Pre-increment causes the value of “a” to be increased by “1” first and only used afterwards.

Java operators for comparisons

Comparisons 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

Description

<

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 mathematician and philosopher 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 programming and digital technology.

Additional 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

Description

!

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 manipulate individual 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 corresponding bit of the second operand.

Java operator

Description

~

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 combination tool for Java operators, assignment operators

Assignment operators are an important tool for assigning values to variables. In each case, an expression is evaluated and applied to the front variable. The simplest variant is a = b, where the value of b is assigned to a. The assignment operators often contribute to shorter lines and can be combined with arithmetic, logical and bitwise Java operators.

Java operator

Description

=

The simple assignment 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.

*=

Multiplication assignment 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.

We use cookies on our website to provide you with the best possible user experience. By continuing to use our website or services, you agree to their use. More Information.