Support Online
Skip to main content

How to Use Java Operators? (Single, Double and Triple)

Operators, one of the most basic topics of Java programming, enable operations on variables and values.
In this guide, you will learn unary, Binary and Ternary operators in practice.
You will also test arithmetic, assignment, relational, logical and precedence rules in the JShell environment.

⚙️ Required Prerequisites

The following components must be ready to try the codes:

  • Java 11+ (JDK) must be installed
  • JShell (REPL) must be running in the terminal
jshell

You can open it with the command and exit by typing /exit.


1️⃣ Unary Operators

Unary operators operate on a single operand and often simplify code.

🔹 Increase (++) and Decrease (--)

int cevap = 42;
System.out.println("Ön Artırma: " + ++cevap);

➡️ Pre-increment: increases the value first and then uses it.

int cevap = 42;
System.out.println("Son Artırma: " + cevap++);
System.out.println("Nihai değer: " + cevap);

➡️ Post-increment: uses the value first and then increases it.

🔹 NOT Operator (!)

boolean bulutAktif = !true;
System.out.println(bulutAktif);

➡️ As a logical complement operator, it inverts the boolean value.

For example:

String node = "tr1-node01";
boolean uygunDegil = !node.contains("tr1");

➡️ Returns true if it does not contain “tr1”.


2️⃣ Binary Operators

Binary operators operate on two operands. This group includes arithmetic, assignment, comparison and logical operators.

🔸 Arithmetic Operators

int toplam = 30 + 12;
System.out.println("Sonuç: " + toplam);

➡️ Performs addition, subtraction (-), multiplication (*), division (/) and mod (%) operations.

🔸 Assignment and Compound Assignment

int kredi = 100;
kredi += 20; // kredi = kredi + 20
System.out.println("Yeni Kredi: " + kredi);

➡️ +=, -=, *=, /= shortens the code, preserves the meaning.

🔸 Casting (Genre Conversion)

int buyukDeger = 32768;
short kisaDeger = (short) buyukDeger;
System.out.println(kisaDeger);

➡️ If the capacity is exceeded, overflow occurs (32768 → -32768).


3️⃣ Relational Operators

Compares two values and returns boolean (true/false).

OperatorDescription
==Is it equal
!=Isn't it equal
>greater than
<is less than
>=Greater than or equal to
<=Less than or equal to
System.out.println(4 > 5); // false
🧩 Referans Tiplerde Eşitlik: == vs equals()
String s1 = new String("evet");
String s2 = new String("evet");
System.out.println(s1 == s2); // false
System.out.println(s1.equals(s2)); // true

➡️ == compares the memory address, ➡️ equals() checks the equality of values.

🔸 instanceof Operator

String mesaj = "merhaba";
System.out.println(mesaj instanceof String); // true

➡️ Checks whether the object is an instance of a particular class.


4️⃣ Logical Operators

Operates on boolean values.

OperatorDescription
&&AND (AND) — If both conditions are true true
`
!NOT (NOT) — Inverts boolean value
boolean javaEglenceli = true;
Boolean nullDeger = null;
System.out.println(javaEglenceli || nullDeger);

➡️ || It does not evaluate the right side thanks to the short-circuit. Normal | If you used it you would get NullPointerException.


5️⃣ Ternary Operator

Returns one of two values depending on the condition.

boolean javaGuzel = true;
String cevap = javaGuzel ? "evet" : "hayır";
System.out.println("Java öğrenmeli miyim: " + cevap);

➡️ If true, it returns “yes”, if false, it returns “no”.


6️⃣ Operator Priority (Precedence)

If there is more than one operator, priority order comes into play.

PriorityOperator(s)Description
1++, --Pre/post increment and decrement
2*, /, %Multiplication, division and modulation
3+, -Addition and subtraction
4<, >, <=, >=Comparison (large/small)
5==, !=Equality and non-equality check
6&&Logical AND (AND)
7`
8=, +=, -=, *=, /=Assignment and compound assignment operators
int x = 1 + 10 / 2;
System.out.println(x); // 6

➡️ First 10/2, then addition. Always use parentheses in complex expressions for clean code:

int x = 1 + (10 / 2);

💬 Frequently Asked Questions (FAQ)

  1. What does the instanceof operator do?

It is used to determine whether an object is an instance of a particular class or interface.

  1. Can ++ be used in final variables?

No, the final variable is fixed and its value cannot be changed.

  1. What is the difference between == and equals()?

== compares address, equals() compares value.

  1. What is jshell?

It is a REPL tool that comes with Java 9. It runs the codes instantly and shows the result.

  1. Why is short-circuit important?

It eliminates the risk of errors (NullPointerException) in null variables.


✅ Result

In this guide, you learned all the operator types (single, binary, ternary) and precedence rules in Java. You can now use arithmetic, logical and comparison operations correctly and write clean code.

☁️ Take it a step further: Test these examples in your own Java environment on GenixNode and supercharge your learning!