This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you.... i truly am a greenhorn...
 
Campbell Ritchie
Sheriff
Pie
Posts: 47244
52
  • 0
  • 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: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • 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
  • 0
  • 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: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • 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