What is a conditional operator in Java
Question mark and colon in Java
- Use question marks and colon operators in Java
- Use nested question marks and colon operators in Java
This tutorial introduces the use of question marks and colon operators in Java and lists some sample code to help understand the subject.
The question mark and colon operators are collectively referred to as ternary operators in Java because they work with three operands.
It is the shorthand version of the instruction in Java and can be used as a one-line instruction for decision making. Let's look at some examples.
Use question marks and colon operators in Java
The ternary operator consists of three segments. The first is a conditional expression that returns a Boolean value. The second and third values are the values before and after the colon. It returns the value before the colon when the conditional expression is evaluated as. Otherwise the value is returned after. The syntax is below.
See the example below.
We can get the return value of any type from the ternary operator. In the following example, we pass string values and get a returned string value based on the condition.
The following example is a use case of the ternary operator in Java. We use this one-line conditional statement to check that the given string is in lowercase and convert it to uppercase if the string is in lowercase. Otherwise the original string is returned.
This is another use case of the ternary operator where we check whether the given integer is a positive integer or not and return a string value. See the example below.
Use nested question marks and colon operators in Java
In this example, we're using the nested ternary operator to see if we can do it like with the statement. Here we first check whether the given integer is a positive integer, then check whether it is between a given range and return a string value. See the example below.
Related Article - Java Operator
- What causes Activa Roller to smell bad
- Is connectbud com reliable
- What are the best alternatives to DreamHost
- Did a boy ever call you beautiful
- What is the most snobbish town in Massachusetts
- What is conventional paving bitumen
- How do I grow a radio audience
- Where can I start to learn an instrument?
- How do you get nerves out of steel
- What gets rid of a rash overnight
- What is two thirds of 18
- How are stress and hallucinations related?
- What happens when the current transformer is saturated
- What's your favorite iPhone app
- What are some effective google search methods
- What are the basics of playing baccarat
- How to choose a man for marriage
- What is gas oil made of
- What is true loves kiss
- What was the tallest Roman building
- Is there a Google AMP CMS
- Have you tried Speed Height capsules before?
- The GOP becomes neo-fascist
- When is music useful?