wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes pls explain this notation (i think it is  an IF STATEMENT Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "pls explain this notation (i think it is  an IF STATEMENT" Watch "pls explain this notation (i think it is  an IF STATEMENT" New topic
Author

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

Christopher Whu
Ranch Hand

Joined: Sep 03, 2008
Posts: 80
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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()."


[Jess in Action][AskingGoodQuestions]
Christopher Whu
Ranch Hand

Joined: Sep 03, 2008
Posts: 80
thank you.... i truly am a greenhorn...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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

Joined: Jul 11, 2001
Posts: 14112
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...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

Right. String.valueOf returns "null" (a String) if the operand is null.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
 
subject: pls explain this notation (i think it is an IF STATEMENT
 
Similar Threads
Netscape7.1 and window.createPopup()
Why we live?
onload
input type=file
build.xml question