aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes EL's treatment of empty string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "EL Watch "EL New topic
Author

EL's treatment of empty string

Jean Robillard
Greenhorn

Joined: Apr 07, 2008
Posts: 17
<% request.setAttribute("bab", ""); %>

${bab eq 0} => outputs false
${bab gt -1} => outputs true

How can I explain this? In a logical expression, an empty string is not treated as 0, because

${6 / bab} => results in an exception, not "Infinity"

How does EL treat an empty string in a logical or arithmetical expression ?

Thanks.
Bobby Sharma
Ranch Hand

Joined: Mar 18, 2008
Posts: 550
    
    1

I am confused too, please someone explain.


Back to Java , again.
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
Sections 2.3.5 (operators) and 2.8 (type conversions) in the JSP Specification are your friends here.

I'll try to follow them through and see what I get. First ${bab eq 0}. This is of type A == B so section 2.3.5.7 applies. The weakness of the spec here is it doesn't say how an IntegerLiteral (here 0) is to be interpreted -- is it a BigInteger, or long or what? I'll go with BigInteger and see what happens... Since A and B are non-null, and B is an IntegerLiteral (BigInteger?), then type-coerce A into a BigInteger and apply A.equals(B). Section 2.8.3 says "If A is null or empty string, return 0". So now A is 0, a BigInteger (we guess). Then A.equals(B) should return true, since both sides are the BigInteger 0. This also works if IntegerLiteral is supposed to be a Long (say), and we still get true.

Since A is being coerced into a 0 value, the second expression ${bab gt -1} should also return true, which it does in your case.

Finally, ${6 / bab}. Once again, we have a choice -- is an IntegerLiteral a BigInteger or Long? In the first case, we coerce both to BigDecimal and return A.divide(B). This throws ArithmeticException since dividing by 0. try the other way, and coerce both to Double and apply the operator. This time, doing a double division by zero gives, yet again, an ArithmeticException. I assume this is the exception you describe?

So the only result I can't see working is the first -- as far as I can tell, ${ bab eq 0 } should be true. Are you sure that was the EL you tried, and if so what container and version was it on? It could be bug, unless I've misread the specs (and feel free to re-analyse the problem and tell me I made a mistake!).

P.S. So you keep your sanity, I very much doubt these very tricky coercions will appear on the exam!


Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
Section 2.8.3 says "If A is null or empty string, return 0".
Reading that important bit again, I really see no reason why the first statement ${bab eq 0} shouldn't be true. According to the JSP 2.0 spec, the empty string is pretty much treated as 0 in any numerical comparison (by the coercion rules of the operations, it is not 0 in non-numerical comparisons, so you have to be a bit careful there).
Bobby Sharma
Ranch Hand

Joined: Mar 18, 2008
Posts: 550
    
    1

thanks Charles for your time.
Vishal Chugh
Ranch Hand

Joined: Oct 27, 2007
Posts: 177
Hi Charles,

I have just started practicing EL , i have below JSP :

<%@ page isErrorPage="true" isELIgnored="false" %>
<html>
<body>

<% request.setAttribute("bab", ""); %>
${bab eq 0} </br>
${bab gt -1} </br> </br>


${6 / bab} </br>
${6 + bab} </br> </br>


${bab && true } </br>
${bab && false } </br></br>

${bab || true} </br>
${bab || false} </br>


</body>
</html>




But i get below output as expected by you :
true
true

Infinity
6

false
false

true
false

So we can conclude that :
If A is null or empty string, return 0 in case of arithematic and relational operators and false in case of logical operaters
Vishal Chugh
Ranch Hand

Joined: Oct 27, 2007
Posts: 177
Additional :

I have doubt about infinity behaviour in case of relational operators (gt, lt, eq ) , earlier i thought it is being treated as false but ${infinity eq false} gave me false :

For infinity we have below behaviour :


${5} -- 5
${infinity} </br>

${infinity + 5} -- 5
${infinity/5} --0.0
${5/infinity} </br> --infinity

${infinity gt 5}-- false
${infinity lt 5}-- false

${infinity eq 0} </br>-- false
${infinity eq false} -- false


${infinity && true}-- false
${infinity && false} </br>-- false

${infinity || true}--true
${infinity || false}-- false

In case of arithematic , inifinity act as 0
and
in case of logical (&& ,|| , not) , we can treat infinity as false as per above outputs , Please guide in case of relational operators
.

[ December 28, 2008: Message edited by: Vishal Chugh ]

[ December 28, 2008: Message edited by: Vishal Chugh ]
[ December 28, 2008: Message edited by: Vishal Chugh ]
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
Check your other thread where I explain there is no EL constant called "infinity"---you're in fact referencing a scoped attribute which doesn't exist.
Vishal Chugh
Ranch Hand

Joined: Oct 27, 2007
Posts: 177
Hi,

Sorry
If i consider infinity as an attribute which doesnt exist , then i agree/disagree for below marked .


${infinity} </br> -Agree

${infinity + 5} -- 5 - Agree , here it will be taken as zero being unknown
${infinity/5} --0.0 -Agree, same reason as above
${5/infinity} </br> --infinity -Agree, same reason as above

${infinity gt 5} -- false - Agree , here it will be taken as zero being unknown

${infinity && true} -- false - Agree , here it will be taken as fasle being unknown
${infinity && false} </br> -- false - Agree , here it will be taken as fasle being unknown


${infinity || true} --true -Agree , here it will be taken as fasle being unknown

${infinity || false} -- false -Agree , here it will be taken as fasle being unknown

I disagree about below :
${infinity eq 0} </br> -- output is false , it should be true
${infinity eq false} -- output is false , it should be true
${infinity lt 5} -- false - it should be true , as zero less than 5



Please guide

[ December 28, 2008: Message edited by: Vishal Chugh ]
[ December 28, 2008: Message edited by: Vishal Chugh ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: EL's treatment of empty string
 
Similar Threads
Apache and BEA Weblogic
SPLIT METHOD IN STRING CLASS
Arrays
Regular Expression Problem in java
Regular expression