- 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
What is the conditional operator ?: in Java?
The conditional operator is also known as the ternary 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
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
Value of b is: 30 Value of b is: 20
- Related Articles
- What is Conditional Operator (?:) in JavaScript?
- What is a Ternary operator/conditional operator in C#?
- Conditional ternary operator ( ?: ) in C++
- How to use the ?: conditional operator in C#?
- How to implement ternary conditional operator in MySQL?
- How do I use the conditional operator in C/C++?
- What is dot operator in Java?
- Does Python have a ternary conditional operator?
- What is instanceof operator in Java? Explain.
- What is Conditional Branching?
- Find largest element from array without using conditional operator in C++
- What is conditional compilation in C language?
- What is the difference between equals() method and == operator in java?
- Maximum of four numbers without using conditional or bitwise operator in C++
- Java Numeric Promotion in Conditional Expression

Advertisements