Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

pls explain this notation (i think it is an IF STATEMENT

 
Christopher Whu
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it's a sort of "if" statement called the "ternary operator". The part before the "?" is the condition, and the parts before and after the ":" are the true and false results, respectively. Both results have to produce a value and they have to be type-compatible. The whole expression says "if value is null, use ""; else use value.toString()."
 
Christopher Whu
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you.... i truly am a greenhorn...
 
Campbell Ritchie
Sheriff
Pie
Posts: 50225
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will find that operator called the ternary operator (because it has three operands) or the conditional operator; I have googled and found a few links which appear useful: 1 (but the () quoted are not an essential part of the operator) 2 and there are bound to be many others.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Incidentally: you ought never write this snippet in real code, because there's a standard method that does exactly this: the static method String.valueOf():

 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
Incidentally: you ought never write this snippet in real code, because there's a standard method that does exactly this: the static method String.valueOf():



In fact, it doesn't produce the same result...
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right. String.valueOf returns "null" (a String) if the operand is null.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic