-2

So how is the Java (@Android) ternary operator supposed to behave really ?

Check these simple examples:

boolean trigger  = true;
String myStr_NOK = trigger  ? "YES:" : "NO:"
                 + " if trigger was TRUE"
                 + " this is NOT visible :-o");

String myStr_OK = (trigger  ? "YES:" : "NO:")
                 + " if trigger was TRUE"
                 + " this is visible as should.");

// I think the first comments missed this fact:
trigger = false;
String myStr_NOK = trigger  ? "YES:" : "NO:"
                 + " if trigger was FALSE"
                 + " this IS concatenated AGAIN");

Now that is a masterpeace of Java logic, that my brain just refuses to swallow.

Do you REALLY have to encapsulate the ternary operator in Java (Android) in brackets, else you do not get the rest of composed string ?

See: myStr_NOK only contains "YES:", since the ternary operator was not in brackets ?

In this page, it clearly states, that no brackets are required. As I would rightfully expect. Quote:

Note that the parentheses in this example are optional, ...

EDIT: Let me re-phrase my Q: What does the standard say about ternary operator ? My observation is such, that withOUT parenthesis around the ternary operator, the further string concatenation is performed ONLY when expression is FALSE. If TRUE, it does not concatenate. Does anyone find this meaningful, obvious and expected behavior ?

And now my original Q that got downvotes: What am I getting wrong, or has the world just gone mad ?

Sold Out
  • 1,321
  • 14
  • 34
  • 1
    The world is just as mad is it has always been. You're misreading that article, the parens it is talking about are not the same as the ones you're trying to omit. Look up operator precedence rules if you want to know why it is behaving as it is. – Mat Aug 25 '17 at 14:27
  • Thanx for hint. Did I get downvote for getting rightfully indignant, of that I happend not to find the right answer myself ? Just to know whether I have no right to ask already aswered Q, or I have no right to get emotional about Java standard... – Sold Out Aug 25 '17 at 14:28
  • No way to tell why you got a downvote. But removing frills from your posts will reduce the risk of downvotes from people who don't like the style. Keep it simple and technical. Also 99.999% of the time "is Java (or C++ or whatever) broken?" questions are answered by "no, you're misunderstanding". So avoid that formulation also. – Mat Aug 25 '17 at 14:33

1 Answers1

2

It does not misbehave at all.

Your first example is basically this written out:

if(true) "YES";
else "NO" + theOtherStrings;

Your second example is written out like this

String string;
if(true) string = "YES";
else string = "NO";
string = string + theOtherStrings
Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107
  • I don't thing I got what you mean. – Sold Out Aug 25 '17 at 14:32
  • @Peter The ternary operator in the first example misses the brackets which means that the first String consist of `YES` while the second String consists of `NO + all the other strings following`. See my edit. – Murat Karagöz Aug 25 '17 at 14:36
  • So what you basically say, is that the standard is such, that when there are no parenthesis around ternary op., then it concatentates the string beyond self ONLY WHEN expression is FALSE ? (When expression is TRUE, then furhter string contatenation works only with brackets.) So this is the supposed behaviour acoording to Java standard ? Cuz that is what I do observe.. – Sold Out Aug 25 '17 at 15:36