Java Tutorial

Java Operators

Operators combine values: arithmetic to compute, comparison and logic to decide, assignment to store. Precedence decides what runs first.

Arithmetic

+, -, *, /, and % work on numbers.% is the remainder after division — the everyday tool for "is this even?" and "wrap around". Remember that int division truncates.

Example

public class Main {
  public static void main(String[] args) {
    System.out.println(7 + 3);    // 10
    System.out.println(7 * 3);    // 21
    System.out.println(17 / 5);   // 3   (truncated)
    System.out.println(17 % 5);   // 2   (remainder)
  }
}

+ does double duty: with numbers it adds, with a String on either side it joins text. "Score: " + 5 gives "Score: 5".

Comparison

==, !=, <, >, <=, and>= each produce a boolean. You use them in if and whileconditions.

Example

public class Main {
  public static void main(String[] args) {
    System.out.println(3 > 1);    // true
    System.out.println(3 == 3);   // true
    System.out.println(3 != 4);   // true
  }
}

For numbers and boolean, == compares values. For objects like String,== compares identity, not contents — use .equals() there instead.

Logical operators

&& (and), || (or), and ! (not) combine boolean conditions. Both&& and || short-circuit: Java stops evaluating as soon as the answer is certain, which lets you guard against errors.

Example

public class Main {
  public static void main(String[] args) {
    int age = 20;
    boolean member = false;
    System.out.println(age >= 18 && member);   // false
    System.out.println(age >= 18 || member);   // true
    System.out.println(!member);                // true
  }
}

Short-circuiting is a safety tool: s != null && s.length() > 0 never callslength() on a null, because the left side fails first and the right side is skipped.

Increment, decrement, and compound assignment

n++ adds one, n-- subtracts one. The compound forms +=, -=,*=, /=, %= update a variable in place: n += 2 meansn = n + 2.

Example

public class Main {
  public static void main(String[] args) {
    int n = 10;
    n += 5;      // 15
    n--;         // 14
    n *= 2;      // 28
    System.out.println(n);
  }
}

Precedence: what runs first

Java evaluates operators in a fixed order, much like maths class: *, /, and% before + and -; arithmetic before comparison; comparison before&& and ||. When in doubt, add parentheses — they cost nothing and make intent obvious.

Example

public class Main {
  public static void main(String[] args) {
    System.out.println(2 + 3 * 4);       // 14, not 20
    System.out.println((2 + 3) * 4);     // 20
  }
}

Try It Yourself

Exercise: Write a single boolean expression that is true when an integer year is a leap year: divisible by 4, but not by 100 — unless it is also divisible by 400.

Show solution
boolean leap = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);

The parentheses and short-circuiting make the rule read almost like the sentence: divisible by 4 and not by 100, or divisible by 400.

Key Takeaways

  • % is the remainder; + also joins strings.
  • Comparisons give a boolean; use .equals() for object contents, not ==.
  • && and || short-circuit — handy for null-safety guards.
  • * / % bind tighter than + -; parenthesize when unsure.

Worked examples

The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.

Open them in the Java editor at /java/try. Change one measurement and check whether the result still has the right unit.

Physics

Power in a resistor

Electrical power in a resistor is I²R. Square the current, multiply by resistance, print watts.

P = I²R

Example

public class Main {
  public static void main(String[] args) {
    double I = 0.050;
    double R = 100.0;
    System.out.println(I * I * R + " W");
  }
}

FAQ: Java Operators

Common questions about this page.

What is the StudyGrid Java tutorial?

The StudyGrid Java tutorial is a full beginner-to-advanced track: syntax, types, input, loops, methods, classes, collections, generics, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run java operators examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn java operators in this Java Java lesson (Java Operators).

Is the Java editor the same as Try Python or Try C++?

No. Try Java compiles with javac at /java/try and shows stdout plus compiler messages. Try Python stays at /try. Try C++ stays at /cpp/try. Java lessons never open those editors.

Do I need to install a JDK to learn Java?

No. Open a chapter, click Try it in Java, and compile in the browser. You can also download a .java file and compile locally with javac.

Where should I start the Java tutorial?

Start at Java Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open Java Examples, then generics, map, and lambdas. Use Next at the bottom of each chapter.

Is the Java tutorial free?

Yes. The Java workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.