DEV Community

Kanishka Shrivastava
Kanishka Shrivastava

Posted on

#java #programming #beginners #100daysofcode

Java Concepts I’m Mastering –> Part 1: The Ternary Operator

I’ve started a personal challenge:
To deeply understand core Java concepts while building real projects.

This is Part 1 of my “Java Concepts I’m Mastering” series.

Today’s focus: The Ternary Operator

What It Is

A compact alternative to if-else.

Syntax:

condition ? value_if_true : value_if_false;

Enter fullscreen mode Exit fullscreen mode

Example

int a = 10;
int b = 20;

int max = (a > b) ? a : b;

System.out.println(max);

Enter fullscreen mode Exit fullscreen mode

Clean. Direct. Efficient.

Nested Example

int a = 10, b = 25, c = 15;

int greatest = (a > b)
                ? (a > c ? a : c)
                : (b > c ? b : c);

Enter fullscreen mode Exit fullscreen mode

Small concept —> but powerful when used correctly.

What I Learned

Use ternary for simple assignments.

Avoid over-nesting.

Readability > cleverness.

I’m currently strengthening Java fundamentals while working on real projects.

Next in the series: Static vs Instance Variables.

Stay tuned

Top comments (0)