Tutorial Gateway

  • C Programming
  • Java
  • R
  • SQL
  • MySQL
  • Python
  • BI Tools
    • Informatica
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • QlikView
  • About
    • About Us
    • Contact Us

Java Ternary Operator

17-03-2016 by suresh Leave a Comment

The Java Ternary Operator is also called as Conditional Operator. The Ternary Operator in Java programming is mostly used in decision making process. This operator returns the statement depends upon the given expression result. Let’s see the syntax of the conditional operator

Java Ternary Operator Syntax

The basic syntax of a Ternary Operator in Java Programming is as shown below:

1
Test_expression ? statement1: statement2

If the given test condition is true then it will return statement1 and if the condition is false then statement2 is returned.

Java Ternary Operator example

In this Java Program, we are going to use the Java Conditional Operator to find whether the person is eligible to vote or not.

JAVA CODE

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Java Ternary Operator Example
package JavaOperators;
 
import java.util.Scanner;
 
public class TernaryOperator {
private static Scanner sc;
 
public static void main(String[] args) {
int age;
sc = new Scanner(System.in);
System.out.println(" Please Enter Your Age: ");
age = sc.nextInt();
String Message = (age >= 18)? " You are eligible to Vote " :
      " You are Not eligible to Vote ";
System.out.println(Message);
/** REPLACE THE ABOVE CODE WITH FOLLOWING CODE
* System.out.println((age >= 18)?
* " You are eligible to Vote":
                 *       "You are Not eligible to Vote");
*/
}
}

ANALYSIS

This program allows the user to enter his or her age and assign the user entered integer value to age variable. If user entered value is 18 or above then it will assign the first statement after the ? symbol to String variable Message

1
" You are eligible to Vote "

If he / she enters below 18 then second statement (which is after the : symbol) will be assigned to String variable Message.

1
" You are Not eligible to Vote ";

Finally, we are using the System.out.println statement to print the string data inside the Message variable

1
System.out.println(Message);

OUTPUT

Java Ternary Operator 1

Let us try with different value

Java Ternary Operator 2

Java Ternary Operator example 2

In this Java Program, we are going to use the Nested Conditional Operator in Java to find whether the person is eligible to work or not.

JAVA CODE

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Java Ternary Operator Example
package JavaOperators;
 
import java.util.Scanner;
 
public class ConditionalOperator {
private static Scanner sc;
 
public static void main(String[] args) {
int age;
sc = new Scanner(System.in);
System.out.println(" Please Enter Your Age: ");
age = sc.nextInt();
 
String Message = (age < 18)? " You are too Young to Work " :
                             (age >= 18 && age <= 60)? " You are eligible to Work ":
                                   " You are too Old to Work ";
System.out.println(Message);
}
}

ANALYSIS

  1. We declared a string variable called Message and we assigned this variable to the conditional operator functionality.
  2. First condition check whether age is less than 18. If this condition is True then it will return first value after the ? symbol, which is You are too Young to Work
  3. If the first condition fail then it will execute the variable after the : symbol. By using Nested conditional operator we are checking one more condition here (age >= 18 && age <= 60). If this condition is True then it will return first value after the ? symbol, which is You are eligible to Work
  4. If the Nested condition fails then it will execute the variable after the : symbol, which is You are too Old to Work.

OUTPUT 1

Java Ternary Operator 3

OUTPUT 2

Java Ternary Operator 4

OUTPUT 3

Java Ternary Operator 5

Thank You for Visiting Our Blog

Placed Under: Java Tutorial

Share your Feedback, or Code!! Cancel reply

Trending

  • SQL Server Integration Services (SSIS)
  • SQL Server Tutorial
  • C Program to Print Prime Numbers from 1 to 100
  • C Program to Calculate the Sum and Average of n Number
  • SQL Server Reporting Services (SSRS)
  • Home
  • Types of Functions in C

Stay in Touch!

Sign Up to receive Email updates on Latest Tutorials

  • Blogger
  • C Programs
  • Java Programs
  • SQL
  • SSIS
  • SSRS
  • Tableau
  • JavaScript

Copyright © 2018 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy