Assignment, Arithmetic And Unary Operators in Java

This post shows the assignment, arithmetic and unary operators available in Java.

Assignment operator in Java

Assignment operator in Java is the “=”. It assigns the value on its right to the operand on its left.
For example-

Assignment operator can also be used on objects to assign object references. For example –

Arithmetic operators in Java

The arithmetic operators available in Java for basic mathematical operations are addition (+), subtraction (-), division (/), multiplication(*) and modulus (%). Modulus operator divides one operand by another and returns the remainder as its result.

Operator Description
+ Additive operator (also used for String concatenation)
Subtraction operator
* Multiplication operator
/ Division operator
% Remainder operator

Java arithmetic operators example


Overloaded + operator for String

The + operator is overloaded in Java and it can also be used for concatenating (joining) two strings together.


Compound assignment in Java

You can also combine the arithmetic operators with the assignment operator to create compound assignments in Java. For example x*=5; is equal to x = x * 5;

Java Compound assignment example


Unary operators in Java

The unary operators in Java require only one operand; they perform various operations such as incrementing/decrementing a value by one, negating an expression, or inverting the value of a boolean.

Operator Description
+ Unary plus operator; indicates
positive value (numbers are
positive without this, however)
Unary minus operator; negates
an expression
++ Increment operator; increments
a value by 1
Decrement operator; decrements
a value by 1
! Logical complement operator;
inverts the value of a boolean

Java Unary operators example


Increment and Decrement operators in Java

Unary operators increment and decrement operators in Java are special as they can be applied after the operand (postfix) or before the operand (prefix) i.e. a++; and ++a; both are valid and result in incrementing the value of a by 1.

Difference between these two forms is that the prefix version (++a) evaluates to the incremented value, whereas the postfix version (a++) evaluates to the original value and then incremented. If you are using this operator in part of a larger expression or assigning it to another variable then which form is chosen may make a difference.


As you can see here a++ evaluates to the original value which is assigned to b and then a is incremented.


Here ++a; evaluates to the incremented value which is assigned to b.

That’s all for the topic Assignment, Arithmetic And Unary Operators in Java. If something is missing or you have something to share about the topic please write a comment.

You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.