Conditional operators in Java

Java — Conditional Operators

Visual article with examples: if / else / logic / ternary operator / switch

In brief — conditional operators allow the program to make decisions: to execute one piece of code or another depending on the expression. Below are compact explanations and examples that you can copy and run in your environment.

The main idea: "if the condition is true — do one, otherwise — do the other." This is how programs become smarter.

1) Simple if / else

Check equality and output the result.


// example: simple if/else
int a = 1;
if (a == 1) {
    System.out.println("Условие выполняется: a == 1");
} else {
    System.out.println("Условие не выполняется");
}
    

Output when a = 1: Condition is met: a == 1

2) else if — chain of checks

When you need to check several options one after another.


int a = 2;
if (a == 1) {
    System.out.println("a == 1");
} else if (a == 2) {
    System.out.println("a == 2");
} else {
    System.out.println("none of the options");
}
    

Tip: the order is important — the first matching if/else if "absorbs" the execution.

3) Comparison Operators and Negation

  • != — not equal
  • >, < — greater / less
  • >=, <= — greater/less or equal
  • ! — logical "not" for boolean expressions

int a = 3;
if (a != 2) {
    System.out.println("a не равно 2");
}
if (a > 2) {
    System.out.println("a больше 2");
}
    

4) Logical operators: && (AND) and || (OR)

Composite conditions.


int a = 3, b = 1;
// logical AND: both conditions must be true
if (a == 3 && b == 1) {
    System.out.println("both conditions are true");
}
// logical OR: it’s enough for at least one to be true
if (a == 2 || b == 1) {
    System.out.println("at least one is true");
}
    

5) Ternary Operator — Compact, but Cautious

Shortened form of if/else. Good for simple assignments, but beginners are better off reading the regular if.


int a = 3, b = 1;
int c = (b == 1) ? (a + 1) : (a - 1);
System.out.println(c);
    

If b == 1, then c = a + 1, otherwise c = a - 1.

6) switch — when there are many discrete options

Easy to read, especially with many integer/string cases. Don't forget break.


int a = 5;
switch (a) {
    case 1:
        System.out.println("a == 1");
        break;
    case 2:
        System.out.println("a == 2");
        break;
    case 3:
        System.out.println("a == 3");
        break;
    default:
        System.out.println("Condition not met");
}
    

7) A small cheat sheet for debugging

  • To understand why it doesn't enter the condition — print the values of the variables before checking.
  • Check types: comparison == for references (objects) — is not the same as for primitives.
  • The ternary operator is good in one line — but do not make complex logic in it.

Test — How well did you understand the lesson?


🌐 На русском
Total Likes:0

Оставить комментарий

My social media channel
By sending an email, you agree to the terms of the privacy policy

Useful Articles:

Go vs Java - comparison of memory models: happens-before, visibility, reorder, synchronization events, write/read barriers
Memory model is a layer between the program and the processor. Modern CPUs aggressively optimize execution: instructions may be reordered, data may be stored in core caches, and operations may be perf...
Context, propagation, and cancellation patterns in Go vs Java | Patterns, idioms, and best practices of Go
← Related articles: Part 1 — Error handling and defer in Go (Concurrency and synchronization) | Patterns, idioms and best practices for Go 1. Context and its role In Go context.Context is used to...
Conditional operators in Java
Java — Conditional Operators Visual article with examples: if / else / logic / ternary operator / switch In brief — conditional operators allow the program to make decisions: to execute one piece of...

New Articles:

Zero Allocation in Java: what it is and why it matters
Zero Allocation — is an approach to writing code in which no unnecessary objects are created in heap memory during runtime. The main idea: fewer objects → less GC → higher stability and performance. ...
Stream vs For in Java: how to write the fastest code possible
In Java, performance is often determined not by the "beauty of the code," but by how it interacts with memory, the JIT compiler, and CPU cache. Let s analyze why the usual for is often faster than Str...
Compiler, Build, and Tooling in Go and Java: how assembly, initialization, analysis, and diagnostics are organized in two ecosystems
This article is dedicated to a general overview of how the compiler, build, and tooling practices are arranged in Go, and how to better understand them through comparison with Java. We will not delve ...
Fullscreen image