1.5 Mastering Operators in Java


22/05/25

8 mins

Mastering Operators in Java: A Beginner’s Guide

One of the first steps to writing useful Java programs is understanding operators. Operators let you do things like add two numbers, compare values, or even shift bits in memory—all with clear and simple symbols. This guide explains how Java operators work, how to use them, and what to watch out for, using details from dev.java's official documentation.

What Are Operators?

Operators are special symbols that trigger specific actions in your code. Think of them as shortcuts for basic (and not-so-basic) operations. For example:

  • + for addition
  • - for subtraction
  • * for multiplication
  • = for assignment

Some operators need one value (unary), some need two (binary), and a few need three (ternary). When several operators appear in one line, Java has rules (called precedence) for which gets handled first.

Operator Precedence

Knowing which operator runs first is important. If you write a + b * c, Java does the multiplication before the addition. Here’s how Java ranks operators (from highest to lowest):

TypeExamples
postfixexpr++, expr--
unary++expr, --expr, +expr, -expr, !expr
multiplicative*, /, %
additive+, -
shift>, >>>
relational``, =, instanceof
equality==, !=
bitwise AND&
bitwise XOR^
bitwise OR`
logical AND&&
logical OR`
ternary? :
assignment=, +=, -=, *= and so on

Assignment Operator (=)

The assignment operator = assigns the value on the right to the variable on the left:

int speed = 0;

You can also use it with objects to assign references.

Arithmetic Operators

Arithmetic operators are just like what you see in math:

OperatorNameExample usage
+Addition, Concatenationa + b or "a" + "b"
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
%Remainder (modulo)a % b

Example:

int result = 1 + 2; // result: 3 result = result * 2; // result: 6 result = result / 2; // result: 3

The + operator also joins strings:

String str = "Hello, " + "world!"; // str: "Hello, world!"

Unary Operators

Unary operators need just one value:

OperatorMeaning
+Positive value
-Negates the value
++Increments by 1
--Decrements by 1
!Inverts a boolean value

Example:

int x = 2; x++; // now 3 x = -x; // now -3 boolean success = false; !success; // true

Prefix (++x) and postfix (x++) both increase by one, but behave differently if used inside other expressions.

Relational & Equality Operators

These compare values:

OperatorDescriptionExample
==Equal toa == b
!=Not equal toa != b
>Greater thana > b
=Greater or equala >= b
` b) ? a : b;
Here, if a > b is true, max gets a; if not, it gets b.

Type Comparison Operator (instanceof)

This checks if an object is an instance of a specific class or interface:

if (myObj instanceof MyClass) { // do something }

Bitwise and Bit Shift Operators

These work on the bit-level, mostly for advanced or low-level coding:

OperatorDescription
~Bitwise NOT
>Right Shift
>>>Unsigned Right Shift
&Bitwise AND
``
^Bitwise XOR

Final Thoughts

Operators allow you to perform everything from everyday calculations to tricky logical tests inside your Java programs. Start by getting comfortable with arithmetic, assignment, and relational operators, and you’ll quickly be able to add more advanced patterns to your projects.

Happy coding!


PREVIOUS: 1.4 Simplifying Java with var

NEXT: 1.6 Expressions, Statements, and Blocks in Java