Comments on: Java ?: operator http://www.terminally-incoherent.com/blog/2006/04/21/java-operator/ I will not fix your computer. Tue, 04 Aug 2020 22:34:33 +0000 hourly 1 https://wordpress.org/?v=4.7.26 By: suvi http://www.terminally-incoherent.com/blog/2006/04/21/java-operator/#comment-21539 Tue, 28 Feb 2012 10:27:38 +0000 http://www.terminally-incoherent.com/blog/?p=500#comment-21539

instead of
int foo = bar ? a : b;
this
var== expression ? a: b;

Reply  |  Quote
]]>
By: Francis http://www.terminally-incoherent.com/blog/2006/04/21/java-operator/#comment-18895 Fri, 15 Apr 2011 12:10:25 +0000 http://www.terminally-incoherent.com/blog/?p=500#comment-18895

Thanks, you turned Chinese into something understandable ;-)

Reply  |  Quote
]]>
By: D http://www.terminally-incoherent.com/blog/2006/04/21/java-operator/#comment-18365 Fri, 28 Jan 2011 02:15:39 +0000 http://www.terminally-incoherent.com/blog/?p=500#comment-18365

Returning null makes Baby Jesus cry.

Reply  |  Quote
]]>
By: Guido http://www.terminally-incoherent.com/blog/2006/04/21/java-operator/#comment-17578 Thu, 28 Oct 2010 09:32:34 +0000 http://www.terminally-incoherent.com/blog/?p=500#comment-17578

Nice explanation, thanks.

Reply  |  Quote
]]>
By: Marcel http://www.terminally-incoherent.com/blog/2006/04/21/java-operator/#comment-14490 Fri, 05 Mar 2010 13:55:18 +0000 http://www.terminally-incoherent.com/blog/?p=500#comment-14490

Thanks, I understand this!

Reply  |  Quote
]]>
By: span http://www.terminally-incoherent.com/blog/2006/04/21/java-operator/#comment-13216 Mon, 21 Sep 2009 06:29:57 +0000 http://www.terminally-incoherent.com/blog/?p=500#comment-13216

Thanks for the clear explanation :)

Reply  |  Quote
]]>
By: wanacas http://www.terminally-incoherent.com/blog/2006/04/21/java-operator/#comment-12348 Tue, 19 May 2009 14:15:30 +0000 http://www.terminally-incoherent.com/blog/?p=500#comment-12348

This works:
System.out.println(boolean ? “Some text” : “Some other text”);

Reply  |  Quote
]]>
By: test http://www.terminally-incoherent.com/blog/2006/04/21/java-operator/#comment-10030 Tue, 02 Sep 2008 10:37:08 +0000 http://www.terminally-incoherent.com/blog/?p=500#comment-10030

another advantage that it will not excute statement1:statement2 only the correct one

Reply  |  Quote
]]>
By: Patrick http://www.terminally-incoherent.com/blog/2006/04/21/java-operator/#comment-7267 Wed, 05 Dec 2007 21:45:06 +0000 http://www.terminally-incoherent.com/blog/?p=500#comment-7267

Ya thanks, it make sense.

Reply  |  Quote
]]>
By: Luke Maciak http://www.terminally-incoherent.com/blog/2006/04/21/java-operator/#comment-7266 Wed, 05 Dec 2007 21:29:00 +0000 http://www.terminally-incoherent.com/blog/?p=500#comment-7266

Well, that is the one way you are not supposed to use the question mark statement. This statement is supposed to evaluate the boolean, and based on it’s value return the result of the statement to the left or the right of the colon.

In other words, the two statements are required to return a value. Since println() is void, I believe this syntax will throw error. So you are right, in your example, the if statement is definitely the correct approach.

To put it more simply, the question mark statement must evaluate to (or return) a value of some sort. It is not supposed to be used to perform an action.

I hope that makes sense. :)

Reply  |  Quote
]]>