- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Ternary Operator Examples
The ternary operator is also known as the conditional operator. This operator consists of three operands and is used to evaluate Boolean expressions. The goal of the operator is to decide, which value should be assigned to the variable. The operator is written as −
variable x = (expression) ? value if true: value if false
Example
Following is an example −
public class Test { public static void main(String args[]) { int a, b; a = 10; b = (a == 1) ? 20: 30; System.out.println( "Value of b is : " + b ); b = (a == 10) ? 20: 30; System.out.println( "Value of b is : " + b ); } }
Output
This will produce the following result −
Value of b is : 30 Value of b is : 20
- Related Articles
- Ternary Operator in Java
- Changing ternary operator into non-ternary - JavaScript?
- Apply Ternary operator on double value in Java
- Ternary Operator in C#
- Ternary Operator in Python?
- C/C++ Ternary Operator
- Java Unary Operator Examples
- Java Arithmetic Operator Examples
- Java Shift Operator Examples
- Java AND Operator Examples
- Java OR Operator Examples
- Java Assignment Operator Examples
- Conditional ternary operator ( ?: ) in C++
- Ternary Operator in Dart Programming
- What is a Ternary operator/conditional operator in C#?

Advertisements