syntax - What is the Java ?: operator called and what does it do? -
I have been working with Java for a few years, but till now I have not run into this build: / P>
int count = ishere? GetHereCount (index): getAwayCount (index); This is probably a very simple question, but can someone explain it? How do I read it? I am pretty sure that I know how it works.
- If
istrue,is called getHereCount (), - if
is called getAwayCount ().
Right?
Yes, this is a short form of
integer count; If (hearer) count = joins the record (index); And count = getAwayCount (index); This is called the conditional operator . Many people (by mistake) call it ternary operator , because it is Java, C, C ++, and possibly many other languages have the only thirteen (three logic) operators. But theoretically may be another threshold operator, while only one conditional operator can be.
The official name is given on:
§15.25 conditional operator? :
conditional operator
?This is a compile-time error for calling zero method for the second or third operator expression.
In fact, by the grammar of expression () (), any conditional expression is not allowed to appear in any context, where one of the zero method Invitation may appear.
Then, if
doSomething ()anddoSomethingElse ()are zero methods, Can not shrink:if (SomeBool) doSomething (); Else doSomethingElse ();In:
someBool? DoSomething (): doSomethingElse ();Simple word:
Boolean condition? ExecuteThisPartIfBooleanConditionIsTrue: executeThisPartIfBooleanConditionIsFalse
Comments
Post a Comment