November 3 2014

Java Ternary Operator

Java Ternary Operator  /  Java Conditional Operator

The ternary operator is also called conditional operator. It is called ternary as it deals with three variables.


variable = (expression1)? expression2 : expression3 ;

expression1 is evaluated first . If expression1 value is true expression2 value will be assigned to variable .If expression1 value is false expression3 value will be assigned to variable.

In the following example (age>=18) is the condition if the expression is true value “Allowed”is assigned to voting else “Not Allowed” will be assigned to voting

Programs : If condition is true

public class TernaryOperator {

public static void main(String[] args) {

int age=18;

String voting;

voting=(age>=18)?"Allowed":"Not Allowed";

// (age>=18) is the condition

System.out.println("Value of voting : "+voting);



Output :

Value of voting : Allowed

Program : If condition is false

Output :

Value of voting : Not Allowed

Copyright © 2014. All rights reserved.

Posted November 3, 2014 by answersz in category Bluej Java Programs for ICSE Class 10 and 9 Practice, Core Java

Leave a Comment

Your email address will not be published. Required fields are marked *