- 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 are the ternary operators 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
- Ternary Operators in C/C++
- What are the unary operators in Java?
- What are the arithmetic operators in Java?
- What are the logical operators in Java?
- What are the bitwise operators in Java?
- What are the relational operators in Java?
- What are the assignment operators in Java?
- Java program to find smallest of the three numbers using ternary operators
- Java program to find largest of the three numbers using ternary operators
- What are the restrictions on increment and decrement operators in java?
- Ternary Operator in Java
- What are the bitwise zero fill right shift zero operators in Java?
- What are the logical operators in C#?
- What are operators in JavaScript?
- Java Ternary Operator Examples

Advertisements