What are ternary operators ( conditional operators ) in Java. Mention the advantage of their use.
Answers
Hey dear here is your answer!!!!!
___________________________
As it's name suggests Ternary Operator is a type of operator used in Java language (as you already mentioned). It is made up of 3 parts of arguments which can be defined in a single line.
It is basically used to provide 3 arguments in your program:-
1. Comparison argument
2. True Argument
3. False Argument
It is a way to reduce the used of 'else-if' statement in your program.
Advantages of using Ternary Operators:-
1. If you use it you won't have to give several lines of program in your program which would include: Condition, True value, False value. Instead you just use ternary operator to do all this work.
2. This reduces the amount of work to write the program, simplifies it, makes it more understandable to the reader.
3. Actually reduces the number of bits you take up to write the program. ( Nothing to worry about actually)
4. It can be nested to provide conditions within other conditions.
5. It is definitely easier to use than 'else-of' statement if mastered.
This is all I could come up with.
Do give your suggestions about my answer and guide me if I missed anything.
❣️⭐ Hope it helps u dear...⭐⭐❣️❣️
HERE'S THE ANSWER ✌
_________________________
⤵⤵⤵⤵⤵⤵
✔What is the Ternary Operator?✔
The ternary operator is an operator that takes three arguments. The first argument is a comparison argument, the second is the result upon a true comparison, and the third is the result upon a false comparison.
If it helps you can think of the operator as shortened way of writing an if-else statement. It is often used as a way to assign variables based on the result of an comparison.
When used correctly it can help increase the readability and reduce the amount of lines in your code.
⚫⚪⚫⚪⚫⚪⚫⚪⚫⚪
✔Why Should I Use the Ternary Operator?✔
The ternary operator greatly increases the conciseness of your code. When it is formatted correctly it can also be very easy to read and, dare I say it, even easier then if-else statements.
There seems to be some controversy over when to use the Ternary Operator and when to use If-Else statements.
⚪⚫⚪⚫⚪⚫⚪⚫⚪⚫⚪
✔Ternary Operator – Properly Formatted✔
//is this year a leap year?
//--------------test expression-----result
$isLeapYear = (($year % 400) == 0) ? 1 :
((($year % 100) == 0)? 0 :
((($year % 4) == 0) ? 1 :
0));//default
✅✅✅✅✅✅✅✅✅
__________________
HOPE YOU FIND IT HELPFUL ☺☺☺☺☺