GoBasics

Java Operators Explained

When you're writing code in Java, you'll often need to do things like add two numbers, check if something is true, or assign a value to a variable. All of that is done using something called operators. Think of operators like special symbols or keywords that perform actions on values or variables.

1. Assignment Operator (=)

This is one of the first operators you'll use in Java. It’s used to assign values to variables.

int a = 10;

Here, you're saying "Hey Java, please put the value 10 into the variable a." This operator does not mean “equal to” like it does in math. It just means you’re storing something in a variable.

2. Arithmetic Operators

These are used to do basic math:

Operator Description
+ for addition
- for subtraction
* for multiplication
/ for division
% for remainder (modulo)
int x = 10, y = 3;
        System.out.println(x + y); // 13
        System.out.println(x - y); // 7
        System.out.println(x * y); // 30
        System.out.println(x / y); // 3
        System.out.println(x % y); // 1
Note: When dividing integers, Java gives you only the whole number part (no decimals). So 10 / 3 is 3, not 3.33.

3. Relational / Comparison Operators

These operators help you compare two values. They return either true or false.

Operator Description
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater or equal
<= Less or equal
int a = 5, b = 10;
        System.out.println(a == b); // false
        System.out.println(a != b); // true
        System.out.println(a < b);  // true
Important: == is used to compare values, not to assign. Beginners often confuse = and ==.

4. Logical Operators

Logical operators are used when dealing with multiple conditions.

Operator Description
&& (AND) – true if both sides are true
|| (OR) – true if at least one side is true
! (NOT) – reverses the result
int age = 20;
        boolean hasLicense = true;

        if (age >= 18 && hasLicense) {
            System.out.println("You can drive!");
        }

5. Unary Operators (++ and --)

These are used to increase or decrease a value by 1.

int a = 5;
        System.out.println(++a); // 6 (pre-increment)
        System.out.println(a++); // 6 (but a becomes 7 after this)
Tip: ++a changes the value before it’s used. a++ uses the value, then increases it.

6. Compound Assignment Operators (+=, -=, *=, /=, %=)

These are shortcuts for arithmetic combined with assignment.

int a = 5;
        a += 5; // same as a = a + 5;

7. Ternary Operator (? :)

This is like a shortcut for if-else. It takes three parts:

condition ? valueIfTrue : valueIfFalse;
int age = 16;
        String message = (age >= 18) ? "Adult" : "Minor";
        System.out.println(message); // Minor

8. Bitwise Operators

Used for bit-level operations, useful in low-level programming.

Operator Description
& (AND)
| (OR)
^ (XOR)
~ (NOT)
<< (left shift)
>> (right shift)

Official Java Documentation: Java Operators Documentation

Common Mistakes Beginners Make

Confusing = and ==

if (x = 5) // wrong! x = 5 is an assignment, not a comparison
Correct: if (x == 5)

Forgetting Integer Division

int result = 5 / 2;
System.out.println(result); // Output: 2, not 2.5
Use at least one double: double result = 5.0 / 2;

Misusing Logical Operators

if (a > 5 & b < 10) // works, but slower
Use && instead of & for efficiency.

Related Posts

Java Methods Explained

Understand how to create reusable blocks of code with Java methods. Learn syntax, parameters, return types, and method overloading.

Read More

Java Control Statements

Explore Java’s control structures like if-else, switch, and loops to manage program flow.

Read More

Java Access Modifiers Explained

Learn how Java's access modifiers (public, private, protected, default) control the visibility and accessibility of classes, methods, and fields.

Read More