Tutorial Gateway

  • C Language
  • Java
  • R
  • SQL
  • MySQL
  • Python
  • BI Tools
    • Informatica
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • QlikView
  • Js

Java Ternary Operator

by suresh

The Java Ternary Operator is also called as Conditional Operator. The Ternary Operator or Conditional operator in Java programming is mostly used in decision making process.

This ternary operator java returns the statement depends upon the given expression result. The basic syntax of a Conditional Operator in Java Programming is as shown below:

Test_expression ? statement1: statement2

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

Java Ternary Operator example

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

// 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 Java Ternary Operator 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

" 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.

" You are Not eligible to Vote ";

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

System.out.println(Message);

OUTPUT

Java Ternary Operator 1

Let us try with different value

Java Ternary Operator 2

Nested Conditional Operator in Java example

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

// Ternary Operator Java 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. In this java ternary operator example, 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. When the first condition in Ternary operator 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 of a conditional operator 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 of the ternary operator

Java Ternary Operator 4

3rd OUTPUT of the conditional operator

Java Ternary Operator 5

Placed Under: Java

Trending Posts

MDX BottomPercent Function

Pie Chart in SSRS

Python Program to Print Hollow Rectangle Star Pattern

Python Program to find Diameter Circumference and Area Of a Circle

Python EXPM1

MySQL LIMIT

SQL CONCAT Function

Create a New Report in SSRS

MySQL Insert Statement

C Program to Calculate Area of a Rectangle

  • C Programs
  • Java Programs
  • SQL FAQ’s
  • Python Programs
  • SSIS
  • Tableau
  • JavaScript

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

Home | About Us | Contact Us | Privacy Policy