This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes != null ? : Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "!= null ? :" Watch "!= null ? :" New topic

!= null ? :

dale conn
Ranch Hand

Joined: Jun 16, 2006
Posts: 57
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

Joined: Mar 18, 2008
Posts: 598

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.

out.println("one is greater than two");

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 ]

Most of the programming problems can't be solved because you don't know what the exact problem is.
Defining your problem correctly leads you to the right solution.
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
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 ]

Bill Shirley - bshirley -
if (Posts < 30) JavaRanchFAQ);
Guido Sautter
Ranch Hand

Joined: Dec 22, 2004
Posts: 142
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

Joined: Jan 30, 2000
Posts: 18671
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.

"I'm not back." - Bill Harding, Twister
I agree. Here's the link:
subject: != null ? :
It's not a secret anymore!