File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "!= null ? :" Watch "!= null ? :" New topic
Author

!= 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: 574
    
    1

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 ]

Back to Java , again.
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 - frazerbilt.com
if (Posts < 30) you.read( 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
Wanderer
Sheriff

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: http://aspose.com/file-tools
 
subject: != null ? :
 
Similar Threads
Values of Hash Map to be 2-dimensional array
URLyBird Search Options
JavaBean Scope in JSP pages
request URI doubt?
Confused: request.getParameter("userName") returns null