- 1 1. Introduction
- 2 2. Overview of Java Operators (with Quick Reference Table)
- 3 3. Explanation and Practical Examples of Each Operator Category
- 3.1 3-1. Arithmetic Operators (+, -, *, /, %)
- 3.2 3-2. Assignment Operators (=, +=, -=, *=, /=, %=)
- 3.3 3-3. Comparison Operators (==, !=, >, <, >=, <=) and instanceof
- 3.4 3-4. Logical Operators (&&, ||, !)
- 3.5 3-5. Bitwise Operators (&, |, ^, ~, <<, >>, >>>)
- 3.6 3-6. Increment and Decrement Operators (++, –)
- 3.7 3-7. Ternary Operator (? 🙂
- 4 4. Operator Precedence and Associativity
- 5 5. Common Errors and Frequently Encountered Pitfalls
- 6 6. Practical Examples: Sample Code Using Operators
- 6.1 6-1. Using Comparison and Logical Operators in if Statements
- 6.2 6-2. Using Increment Operators in Loops
- 6.3 6-3. Simplifying Conditional Assignment with the Ternary Operator
- 6.4 6-4. Simplifying Code with Compound Assignment Operators
- 6.5 6-5. Practical Bitwise Operator Example: Managing Flags
- 6.6 6-6. Combining Multiple Operators in Real Scenarios
- 6.7 6-7. Tips for Writing Readable Code
- 7 7. Summary
- 8 8. FAQ (Frequently Asked Questions)
- 9 9. Reference Links and Official External Resources
1. Introduction
Java is a highly popular programming language used in a wide range of fields, including business systems, web applications, and Android app development. One of the first fundamental elements you will encounter when learning Java is the “operator.” Operators are essential symbols and rules used to perform calculations or comparisons in a program, and they appear frequently in any Java codebase.
Many people searching for the keyword “Java operators” may be facing questions such as:
- Wanting to organize the different types and meanings of operators
- Wanting to see concrete examples of how operators are used
- Wanting to understand the differences and caveats between operators
This article systematically explains the main operators used in Java, covering everything from the basics to practical applications in a clear and beginner-friendly way. It also summarizes common errors, important considerations, and useful tips for real-world development. Mastering operators is the first step toward writing readable and low-bug code.
Whether you’re just starting Java or reviewing the fundamentals, this article aims to be your “go-to reference” when you run into trouble. With examples and diagrams, we will help you fully understand Java operators.
Please read through to the end and solidify your mastery of Java operators.
2. Overview of Java Operators (with Quick Reference Table)
Java offers a wide variety of operators classified by purpose. Here, we organize the representative operators used in Java to help you grasp the big picture. First, let’s look at a quick reference table showing the roles and notation of each operator at a glance.
Java Operator Quick Reference Table
| Category | Operator Examples | Main Usage | Sample Code |
|---|---|---|---|
| Arithmetic Operators | +, -, *, /, % | Numeric calculations | a + b, x % y |
| Assignment Operators | =, +=, -=, *=, /= | Assigning and updating values | x = 5, y += 2 |
| Comparison Operators | ==, !=, >, <, >=, <= | Value comparison | a == b, x >= y |
| Logical Operators | &&, ||, ! | Logical evaluation | (x > 0 && y < 10) |
| Bitwise Operators | &, |, ^, ~, <<, >>, >>> | Bit-level operations | x & y, x << 1 |
| Increment / Decrement | ++, — | Increase or decrease values | i++, –j |
| Ternary Operator | ? : | Conditional value switching | max = (a > b) ? a : b |
| Others | instanceof | Type checking | obj instanceof String |
Java operators are used in various scenarios such as calculations, comparisons, and conditional branching. Arithmetic, assignment, comparison, and logical operators appear in nearly every program.
Bitwise operators, the ternary operator, and the instanceof operator are more advanced, but learning them greatly expands your expressive power in Java.
In the following sections, we will explain each operator category along with practical examples you can use immediately.
3. Explanation and Practical Examples of Each Operator Category
Java provides many different types of operators. In this section, we explain their usage, characteristics, examples, and common pitfalls for each category. Make sure to understand the distinct behavior of every operator type.
3-1. Arithmetic Operators (+, -, *, /, %)
Arithmetic operators are used to perform numeric calculations. They are foundational operations for tasks such as addition, subtraction, multiplication, division, and remainder calculation.
+(Addition): Adds two numeric values. When used with strings, it performs concatenation.-(Subtraction): Computes the difference between two numbers.*(Multiplication): Multiplies two numbers./(Division): Divides the left operand by the right operand. Integer division discards the decimal portion.%(Modulo): Returns the remainder of a division.
Example:
int a = 10;
int b = 3;
System.out.println(a + b); // 13
System.out.println(a - b); // 7
System.out.println(a * b); // 30
System.out.println(a / b); // 3 (decimal part is discarded)
System.out.println(a % b); // 1Notes:
- Division between int values results in an integer output (decimal part discarded).
- Using the
+operator with strings produces concatenation, not arithmetic addition.
3-2. Assignment Operators (=, +=, -=, *=, /=, %=)
Assignment operators are used to set or update the value of a variable. Compound assignment operators help make code more concise.
=(Assignment): Assigns the right-hand value to the variable on the left.+=(Add and Assign): Adds the right-hand value and reassigns the result.- Other compound operators include
-=,*=,/=,%=.
Example:
int x = 5;
x += 3; // Equivalent to x = x + 3 → x becomes 8
x *= 2; // Equivalent to x = x * 2 → x becomes 16Key Point:
- Compound assignment operators are especially useful in repetitive calculations or loop operations.
3-3. Comparison Operators (==, !=, >, <, >=, <=) and instanceof
Comparison operators check whether values meet specified conditions.
==(Equal to): Checks if two values are equal.!=(Not equal to): Checks if two values are different.>,<,>=,<=: Comparison of magnitude.instanceof: Checks whether an object is an instance of a specific type.
Example:
int a = 5, b = 7;
System.out.println(a == b); // false
System.out.println(a < b); // true
String str = "hello";
System.out.println(str instanceof String); // trueImportant Note:
- To compare the contents of strings or objects, use
equals(). The==operator compares references (whether the same instance is referenced).
3-4. Logical Operators (&&, ||, !)
Logical operators are used when you need to evaluate combined conditions.
&&(AND): Returns true only if both conditions are true.||(OR): Returns true if at least one condition is true.!(NOT): Negates a boolean value.
Example:
int age = 20;
boolean isMember = true;
System.out.println(age >= 18 && isMember); // true
System.out.println(!(age < 18)); // trueShort-circuit evaluation:
&&and||skip evaluating the right-hand side if the left-hand condition already determines the result.
3-5. Bitwise Operators (&, |, ^, ~, <<, >>, >>>)
Bitwise operators manipulate integer values at the bit level. They are useful in system development or performance-critical processing.
&(AND): Returns 1 only if both bits are 1.|(OR): Returns 1 if either bit is 1.^(XOR): Returns 1 if only one of the bits is 1.~(NOT): Flips all bits.<<(Left Shift): Shifts bits to the left.>>(Right Shift): Signed right shift.>>>(Unsigned Right Shift)
Example:
int x = 5; // 0101
int y = 3; // 0011
System.out.println(x & y); // 1 (0001)
System.out.println(x | y); // 7 (0111)
System.out.println(x ^ y); // 6 (0110)
System.out.println(~x); // -6
System.out.println(x << 1); // 103-6. Increment and Decrement Operators (++, –)
These operators increase or decrease a variable’s value by 1. Pre-increment and post-increment behave differently.
++: Increases by 1.--: Decreases by 1.
Example:
int i = 0;
i++; // i becomes 1
++i; // i becomes 2Pre vs Post:
++iincrements first, then returns the value.i++returns the current value, then increments.
3-7. Ternary Operator (? 🙂
The ternary operator allows you to write conditional logic in a compact one-line expression.
Syntax:
condition ? value_if_true : value_if_falseExample:
int max = (a > b) ? a : b;Tip:
- It can simplify code, but avoid overusing it for complex conditions.
4. Operator Precedence and Associativity
When multiple operators appear in the same expression, Java evaluates them according to specific rules called “operator precedence.” Additionally, when operators with the same precedence appear together, the order in which they are evaluated is determined by “associativity.”
If you misunderstand these rules, your code may produce unexpected results or bugs.
4-1. Operator Precedence Table
The following table lists major Java operators ranked by precedence. Smaller numbers represent higher precedence.
| Precedence | Operators | Main Usage | Associativity |
|---|---|---|---|
| 1 | () | Grouping with parentheses | Left to Right |
| 2 | ++, --, !, ~, +, - | Unary operators | Right to Left |
| 3 | *, /, % | Multiplication, division, remainder | Left to Right |
| 4 | +, - | Addition, subtraction | Left to Right |
| 5 | <<, >>, >>> | Shift operations | Left to Right |
| 6 | <, <=, >, >=, instanceof | Comparison and type checking | Left to Right |
| 7 | ==, != | Equality and inequality | Left to Right |
| 8 | & | Bitwise AND | Left to Right |
| 9 | ^ | Bitwise XOR | Left to Right |
| 10 | | | Bitwise OR | Left to Right |
| 11 | && | Logical AND | Left to Right |
| 12 | || | Logical OR | Left to Right |
| 13 | ? : | Ternary (conditional) operator | Right to Left |
| 14 | =, +=, -=, other assignment operators | Assignment | Right to Left |
4-2. Visualizing Precedence and Associativity
Consider the following expression:
int result = 2 + 3 * 4;Since * (multiplication) has higher precedence than + (addition), the multiplication is evaluated first:
3 * 4 = 12,
then 2 + 12 = 14.
4-3. Using Parentheses to Explicitly Control Precedence
When an expression becomes complex or you want to ensure clarity, always use parentheses () to explicitly control the order of evaluation.
Example:
int result = (2 + 3) * 4; // 2+3 is evaluated first → result becomes 204-4. Common Mistakes and Important Notes
- Incorrect assumptions about precedence can produce unexpected results.
- Example:
boolean flag = a > 0 && b < 10 || c == 5;- Because
&&has higher precedence than||, this expression is equivalent to:(a > 0 && b < 10) || c == 5
- Because
- To avoid bugs, **always use parentheses for complex expressions**.
Operator precedence and associativity often confuse beginners, but once you understand the rules, you will be able to write far more predictable and reliable Java code.
5. Common Errors and Frequently Encountered Pitfalls
Although Java operators may seem simple, both beginners and intermediate developers often encounter unexpected behaviors and subtle mistakes. This section summarizes common real-world errors and typical pitfalls related to operators.
5-1. Unexpected Results from Integer Division
When dividing two int values in Java, the result is always an integer—any decimal portion is discarded.
int a = 5;
int b = 2;
System.out.println(a / b); // Output: 2If you want a decimal result, cast one of the operands to double (or float):
System.out.println((double)a / b); // Output: 2.55-2. Floating-Point Precision Issues
Using double or float may introduce subtle rounding errors.
double d = 0.1 + 0.2;
System.out.println(d); // Output example: 0.30000000000000004For calculations requiring strict accuracy (e.g., financial values), use BigDecimal instead.
5-3. Difference Between == and equals()
A very common mistake is misunderstanding the difference between == and equals() when comparing objects such as strings.
==: Compares whether two references point to the same instance.equals(): Compares the actual contents (value or text) of the objects.
String s1 = new String("abc");
String s2 = new String("abc");
System.out.println(s1 == s2); // false (different instances)
System.out.println(s1.equals(s2)); // true (contents are identical)5-4. Side Effects Lost Due to Short-Circuit Evaluation
Logical operators && and || use “short-circuit evaluation,” meaning the right-side expression is skipped when the result is already determined by the left side.
Without understanding this behavior, expected side effects (like variable updates or method calls) may never occur.
int a = 0;
if (a != 0 && 10 / a > 1) {
// This block is never executed
}Here, since a != 0 is false, the expression 10 / a is never evaluated, avoiding a division-by-zero error.

5-5. Incorrect Logic Due to Missing Parentheses
Leaving out parentheses in complex conditional expressions often leads to wrong evaluations because of precedence misunderstanding.
boolean flag = a > 0 && b < 10 || c == 5;
// Intended meaning: ((a > 0) && (b < 10)) || (c == 5)
// But depending on context, interpretation may differ5-6. Summary
- Always verify data types (int vs double) and comparison methods (== vs equals).
- Form a habit of using parentheses for complex expressions.
- Be aware of Java-specific behaviors such as short-circuit evaluation.
By keeping these points in mind, you can significantly reduce typical operator-related bugs in Java.
6. Practical Examples: Sample Code Using Operators
This section introduces practical sample code demonstrating how Java operators are used in real development scenarios. These examples highlight common use cases that help deepen understanding and improve practical skills.
6-1. Using Comparison and Logical Operators in if Statements
Comparison and logical operators are essential when combining multiple conditions for branching.
int age = 25;
boolean isMember = true;
if (age >= 18 && isMember) {
System.out.println("Service is available.");
} else {
System.out.println("Conditions not met.");
}6-2. Using Increment Operators in Loops
Increment (++) and decrement (–) operators are frequently used when controlling counters in loop processing.
for (int i = 0; i < 5; i++) {
System.out.println("Count: " + i);
}6-3. Simplifying Conditional Assignment with the Ternary Operator
The ternary operator allows you to assign values without writing a full if statement.
int score = 75;
String result = (score >= 60) ? "Pass" : "Fail";
System.out.println(result); // Pass6-4. Simplifying Code with Compound Assignment Operators
Compound assignment operators are useful when repeatedly updating variable values.
int total = 0;
for (int n = 1; n <= 10; n++) {
total += n; // Equivalent to total = total + n
}
System.out.println("Total: " + total);6-5. Practical Bitwise Operator Example: Managing Flags
Bitwise operations are useful when managing multiple ON/OFF flags efficiently.
int FLAG_READ = 1; // 0001
int FLAG_WRITE = 2; // 0010
int FLAG_EXEC = 4; // 0100
int permission = FLAG_READ | FLAG_WRITE; // 0011
// Check if write permission exists
if ((permission & FLAG_WRITE) != 0) {
System.out.println("Write permission granted.");
}6-6. Combining Multiple Operators in Real Scenarios
When conditions become complex, use parentheses to prevent ambiguity.
int a = 3, b = 7, c = 5;
if ((a < b && b > c) || c == 5) {
System.out.println("Condition satisfied.");
}6-7. Tips for Writing Readable Code
- Break down complex expressions into smaller, more readable parts.
- Use parentheses to explicitly clarify evaluation order.
- Name variables and write comments that clearly convey intent.
Running these sample programs yourself will deepen your understanding of operators. Once you can freely apply operators, Java development becomes both more efficient and enjoyable.
7. Summary
Up to this point, we have covered the major operators used in Java—from basic concepts to practical applications. Operators are fundamental for performing calculations, evaluations, and data manipulation within programs. Understanding and using them correctly enables more efficient and error-free coding.
7-1. Review of This Article
- Java provides many operator types such as arithmetic, assignment, comparison, logical, bitwise, ternary, increment/decrement, and
instanceof, each serving different purposes and behaviors. - Knowing Java-specific rules—such as operator precedence, associativity, and short-circuit evaluation—helps prevent unexpected bugs.
- Learning through practical examples such as
ifstatements, loops, and conditional branching deepens understanding. - It is important to be aware of common errors, such as confusion between data types or using
==instead ofequals()for object comparison.
7-2. Study Advice
The most effective way to learn how operators work is to write code and run it yourself. Try entering and executing the sample code introduced in this article to experience the behavior firsthand.
Whenever you encounter questions or uncertainties, get into the habit of referring to documentation or reliable technical resources to reinforce your understanding.
Mastering the basics of Java operators will give you confidence when working on any Java program. Use this knowledge to support your ongoing learning and development.
8. FAQ (Frequently Asked Questions)
This section covers common questions from learners and working developers regarding Java operators. Use these answers to reinforce your understanding and quickly resolve any doubts.
Q1. Which operator is used to concatenate strings?
A1. The + operator is used for string concatenation.
For example, "Hello" + " World" results in "Hello World".
When concatenating a string with a number, the result becomes a string.
Q2. What is the difference between the == operator and the equals() method?
A2.
==compares whether two references point to the same object instance.equals()compares the content inside the objects.
For objects such as String, always use equals() when you want to compare values.
Q3. What is the difference between prefix (++i) and postfix (i++) increment operators?
A3.
- Prefix (
++i): increments the value first, then returns the updated value. - Postfix (
i++): returns the current value first, then increments.
int i = 5;
System.out.println(++i); // Outputs 6
System.out.println(i++); // Outputs 6, then i becomes 7Q4. What is short-circuit evaluation in logical operators?
A4. Logical operators && and || skip evaluating the right-hand expression if the left-hand side already determines the final result.
This prevents unnecessary computation and avoids potential errors, such as division by zero.
Q5. How can I explicitly change operator precedence?
A5. Use parentheses ().
Parentheses force the enclosed part to be evaluated first, making complex expressions clearer and safer.
int result = (2 + 3) * 4; // 2+3 is evaluated firstQ6. In what situations are bitwise operators useful?
A6. Bitwise operators are helpful in:
- Flag management
- Hardware-level control
- Performance-optimized calculations
For example, they allow multiple ON/OFF states to be stored efficiently in a single integer.
Q7. Can I define my own operators in Java?
A7. Java does not support defining new operators or operator overloading like C++.
However, you can implement equivalent behavior by creating your own methods.
Other questions may arise as you continue practicing. When that happens, refer to official documentation or trusted learning resources to deepen your understanding.
9. Reference Links and Official External Resources
For readers who want to explore Java operators more deeply or verify official specifications, here is a collection of reliable references and learning resources. These links are also useful during actual development or research.
9-1. Official Documentation
- Java SE Documentation (English, Official) Comprehensive details on operator specifications, evaluation rules, and expression behavior.
- Java Platform SE 8 API Documentation (English, Official) Useful when searching for detailed class and method information.
9-2. Helpful External Learning Resources
- Dotinstall – Java Basics (Japanese) Beginner-friendly video tutorials covering Java fundamentals.
- Progate – Java Course (Japanese) Hands-on learning platform for practicing Java fundamentals interactively.
- Qiita – Java Tag Article List (Japanese) Contains practical tips, examples, and up-to-date community knowledge.
9-3. For Those Who Want to Study Further
Notes on Usage
The above links represent key learning resources and official references as of May 2025.
Since content and URLs may change in the future, be sure to check for the latest updates periodically.
By combining these resources with this article, you can further deepen your understanding of Java operators and enhance your practical development skills.

