HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to JavaVariablesData TypesOperatorsStringsArraysLists (ArrayList)Conditions (if / switch)Loops (for / while)MethodsClasses (OOP)
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array & List
🔁 Condition & Loop
🧩 Method & Class
Java›Operators

Operators

Math (+ - * / %), comparison (== < >), logical (&& || !), and string equality (.equals).

Simple enough for a 5-year-old to understand

Math

Example code
1
public class Main {
2
public static void main(String[] args) {
3
System.out.println(7 % 2);
Remainder: 1
CodeRemainder: 1
4
System.out.println(7 / 2);
Integer division: 3
CodeInteger division: 3
5
System.out.println(7.0 / 2);
Decimal: 3.5
CodeDecimal: 3.5
6
}
7
}
Code
💡 Hint: For string equality, use .equals() NOT ==: a.equals(b).

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Print the remainder of 23 divided by 4.

🎯 Expected output: 3

Loading editor…

About this topic

Java operators come from the C family and are familiar to those who know C, C++, C#: arithmetic, modulus, increment and decrement, comparison, logical combination. In everyday code, you will mostly use comparison and logical operators.

The most critical topic is the comparison of objects. Double equals compares the value in primitive types, but in objects it compares the REFERENCE — meaning it asks "is it the same object?". Even if two Strings hold the same text, they can be different objects and double equals will return false. Content comparison is done with equals.

What you will learn

  • Arithmetic and shorthand assignment operators
  • Increment and decrement operators, and the difference between prefix and postfix
  • Comparison operators
  • Logical and, or, not operators
  • The necessity of using equals for object comparison
  • making choices in a single line using the conditional operator

When to use?

The modulus operator is used in odd-even checks and periodic operations. Shortcut assignment operators are standard for counters and accumulating totals in loops.

Short-circuit evaluation is used for safety: if you check whether an object is null and then call its method, if the object is null the second part never runs and you don't get a NullPointerException.

Common mistakes

Wrong

String a = "hello";
String b = new String("hello");
if (a == b) {
    System.out.println("equal");
}

Double equals compares references; a String created with new is a separate object, so the condition does not run. equals must be used to compare text content.

Correct

String a = "hello";
String b = new String("hello");
if (a.equals(b)) {
    System.out.println("equal");
}

Wrong

String input = null;
if (input.equals("evet")) {
    System.out.println("onay");
}

Calling a method on null throws a NullPointerException. Reversing the comparison and doing it through the constant value completely eliminates this risk.

Correct

String input = null;
if ("evet".equals(input)) {
    System.out.println("onay");
}

Wrong

int i = 5;
int result = i++ + i;
System.out.println(result);

Postfix increment increases the value AFTER USING IT; this expression evaluates as 5 plus 6 and gives 11. Incrementing and reading a variable on the same line harms readability and yields different results across languages.

Correct

int i = 5;
i++;
int result = i + i;
System.out.println(result);

Frequently asked questions

Why should I use equals for String comparison?

Because a String is an object and double equals asks "is it the same object?", not "is it the same text?". Since Java shares identical literal texts, double equals returns true in some cases and this is misleading; the code breaks unexpectedly elsewhere.

What is the difference between single and double ampersand?

The double sign short-circuits: if the left side is false, the right side is never evaluated. The single sign evaluates both sides. In everyday code, the double sign is almost always used; the single sign is for bitwise operations.

What is the difference between prefix and postfix increment?

Prefix increments first and then gives the value; postfix gives the value first and then increments. When used alone on a line, there is no difference. The difference only appears when you do the increment inside another expression — and in that case, writing it on a separate line is always clearer.

Summary

Java operators come from the C family; the most critical rule is to use equals for object comparison, because double equals compares references. Writing the constant value on the left for null safety is a classic defensive pattern.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code