- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Apply Ternary operator on double value in Java
The ternary operator is also known as the conditional operator. This operator consists of three operands and is used to evaluate Boolean expressions.
Let’s say we have the following two double values.
double val1 = 20.0; double val2 = 3.7;
Now, let us use the ternary operator to check for the first value.
System.out.println(val1 == 20 ? "Correct!" : "Incorrect!");
If the above condition is correct i.e. val is equal to 20, therefore first statement “Correct” will be returned. If it isn’t equal, then the second statement will get evaluated.
The following is the final example.
Example
import java.util.*; public class Demo { public static void main(String args[]) { double val1 = 20.0; double val2 = 3.7; System.out.println("Checking first value " + val1); System.out.println(val1 == 20 ? "Correct!" : "Incorrect!"); System.out.println("Checking second value " + val2); System.out.println(val2 == 3.8 ? "Correct!" : "Incorrect!"); } }
Output
Checking first value 20.0 Correct! Checking second value 3.7 Incorrect!
- Related Articles
- Ternary Operator in Java
- Java Ternary Operator Examples
- Ternary Operator in Python?
- Ternary Operator in C#
- Changing ternary operator into non-ternary - JavaScript?
- Conditional ternary operator ( ?: ) in C++
- Ternary Operator in Dart Programming
- C/C++ Ternary Operator
- What is a Ternary operator/conditional operator in C#?
- How Ternary operator in PowerShell Works?
- What is ternary operator in C#?
- Apply modulus operator to floating-point values in Java
- What is a ternary operator (?:) in JavaScript?
- How to overload python ternary operator?
- Difference between the Ternary operator and Null coalescing operator in php

Advertisements