What kind of Java syntax is “= = null? False: true;”
•
Java
I'm looking at the code and wondering what this means:
Boolean foo = request.getParameter("foo") == null? false:true;
It must be something that converts the returned string from getparameter () to a Boolean value
But I've never seen such a java with question marks and colons (except in the foreach loop) Any appreciation!
Solution
This is a ternary operator Clip:
Boolean foo = request.getParameter("foo") == null? false:true;
amount to:
Boolean foo; if (request.getParameter("foo") == null) foo = false; else foo = true;
Or (optimized):
Boolean foo = request.getParameter("foo") != null;
The basic form of the operator is:
(condition) ? (value if condition true) : (value if condition false)
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码