How to use if else short method in java? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to use if else short method in java?

3rd Aug 2019, 2:10 PM
Dnyaneshwar Ware
Dnyaneshwar Ware - avatar
5 Answers
+ 13
▪The  ?:  operator in Java The value of a variable often depends on whether a particular boolean expression is or is not true and on nothing else. For instance one common operation is setting the value of a variable to the maximum of two quantities. In Java you might write:   if ( a > b ) {       max = a;   } else {       max = b;   } Setting a single variable to one of two states based on a single condition is such a common use of 'if-else' that a shortcut has been devised for it, the conditional operator ?: Using the conditional operator you can rewrite the above example in a single line like this:     max = ( a > b ) ? a : b; ( a > b ) ? a : b;  is an expression which returns one of two values, a or b.       The condition, ( a > b ), is tested.   • If it is 'true' the first value, a, is returned.   • If it is 'false' , the second value, b, is returned. Whichever value is returned is dependent on the conditional test, a > b. The condition can be any expression which returns a boolean value.
3rd Aug 2019, 4:36 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 8
➝ Remember to use  🔍SEARCH. . .  bar to avoid from posting duplicate threads! https://www.sololearn.com/Discuss/1765175/?ref=app
4th Aug 2019, 8:32 AM
Danijel Ivanović
Danijel Ivanović - avatar
+ 6
here is a reference to the short method via stackoverflow: https://stackoverflow.com/questions/4461996/short-if-else-statement if you are in a hurry to get an answer and alot of programming involved this can speed things up but imo it is still a better practice to us the normal if else method as you can detail / modify and refine it for more specific answers... Java tag should have been in the thread tags
3rd Aug 2019, 3:54 PM
BroFar
BroFar - avatar
+ 2
Can you please state the language involved (Java) for your question Relevant Tags? It would also help you gain more responses if you can describe your question clearly (better with details & example). And what exactly did you mean by 'short method' anyways? are you talking ternary operator?
3rd Aug 2019, 3:24 PM
Ipang
+ 2
Thanks for the answer
4th Aug 2019, 2:32 AM
Dnyaneshwar Ware
Dnyaneshwar Ware - avatar