• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

!= null ? :

 
dale conn
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all

can anyone tell me what this means?

<%=(search != null ? search : "")%>

I know != is not equal to nothing, but what does the ? and the : mean

Many thanks
 
Bobby Sharma
Ranch Hand
Posts: 598
3
Google App Engine jQuery Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
LOL, my dear friend you need to look over conditional operator because the
problem you are asking is not from JSP, you need to read some books
on java's basic.

(1>2)? one is greater than two: loser.

if(1>2)
{
out.println("one is greater than two");
}
else
out.println("loser");

Conditional operator is nothing but alternative of if/else.

[edit: Be nice!]

[ April 17, 2008: Message edited by: omi sharma ]
[ April 17, 2008: Message edited by: Bear Bibeault ]
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The java ternary operator. The only operator that takes three arguments.

First there was C, and it was good, mostly. Then was begat C++ and it was an abomination, somewhat. Then in an attempt to clean it up, we got Java. It was so clean and pure that they modified it. And again. And again. ... (has Java 7 been released, yet)?


This operator is an inheritance of C, but is mostly useful. It should only be used where it aids in code readability!

When viewed in javascript is all the more scary! (But I think the example you showed it a simple and fair use of it.)

This is a fairly common idiom in the language:


So, there's a convenient shorthand:


Of course your example didn't help by explicitly putting parentheses around the condition to clarify the order of operations, and are surrounded by script cruft.

[ April 17, 2008: Message edited by: Bill Shirley ]
[ April 17, 2008: Message edited by: Bill Shirley ]
 
Guido Sautter
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's also a case where the conditional operatior is essential: You cannot wrap calls to a super constructor in if-else-blocks, so if you want to decide at runtime which value to pass to a super constructor, you can do nothing but use the (condition ? x : y) expression. This is useful, for instance, if your constructor takes some Object as an argument and you want to pass a fallback to the super constructor in case the specified argument is null.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good point. Although it's not quite essential in this case - you can also call static methods that have the same effect:

This is legal. However it may be shorter to replace the SomeThing constructor with

So the ternary ? : operator is never absolutely necessary , but it can certainly be convenient.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic