Win a copy of Design for the Mind this week in the Design forum!

# Arithmetic value of "" in EL expressions

Bindu Lakhanpal
Ranch Hand
Posts: 171
In Enthware mock question "com.enthuware.ets.scwcd.v5.2.179"
It is stated in answer and explanation that EL expression
\${requestScope['none'] eq 0} will return true.The reason given is : "" is interpreted as 0.
But I got false as its output (line 1)

output is:

ge true
gt true
eq false
and false
not true
value

Can someone please explain this output and hence the arithmetic value of "".

Vijitha Kumara
Bartender
Posts: 3913
9
Bindu Lakhanpal wrote:Can someone please explain this output and hence the arithmetic value of "".

EL is "null" friendly. But "" is not null so you will get a NumberFormatException. Try commenting the line "request.setAttribute("none",""); " and you will get the output as "1" for the "\${requestScope['none'] + 1}" statement as EL treats the null attribute as 0.

Chinmaya Chowdary
Ranch Hand
Posts: 432
Hi Bindu.
But I got false as its output (line 1)

I tested this in Tomcat5.5.9, Firefox3.5 and I got 'true'.
\${requestScope['none'] eq 0} will return true.The reason given is : "" is interpreted as 0.

I am not getting how "" is interpreted as 0. Can you please explain?

Bindu Lakhanpal
Ranch Hand
Posts: 171
I am using Tomcat 6.0.18. And i got "false".

Hasnain Javed Khan
Ranch Hand
Posts: 73
Hello Bindu.

The following is quoted from the JSP 2 Specs

JSP.2.8.3 Coerce A to Number type N
• If A is null or "", return 0.

Hope that helps.

Kind Regards.

Hasnain.

Chinmaya Chowdary
Ranch Hand
Posts: 432
Hi Hasnain.

JSP.2.8.3 Coerce A to Number type N
• If A is null or "", return 0.

Can you please explain how got '0', its underlying mechanism?

Bindu Lakhanpal
Ranch Hand
Posts: 171
Hi Hasnain ,
Yes.It helped.Thanks.So it means Tomacat 6.0.18 is not behaving as expected from specificaton.And Tomcat 5.5.9 was behaving as expected.
Anyway, i know what is correct for the exam.Its always what spec says.

Hasnain Javed Khan
Ranch Hand
Posts: 73
Hi Chinmaya,

Chinmaya Chowdary wrote:Hi Hasnain.

JSP.2.8.3 Coerce A to Number type N
• If A is null or "", return 0.

Can you please explain how got '0', its underlying mechanism?

Sorry but 'm afraid I did not get your question.

Well....From the top of my head

Is this what were you asking ?

Kind Regards.
Hasnain Javed

Ankit Garg
Sheriff
Posts: 9519
22
Chinmay, read the specs, its given in the specs, for relational operators, if one of the operand is a number, the second is converted into a number too, in that if the second operand is null or empty string, then its converted to 0. Read this page. Specifically these parts

coerce A to Number type N
A is null or ""
return 0

Bindu Lakhanpal
Ranch Hand
Posts: 171
Chinmaya, can you please check the output of following line in tomcat 5.I am just curious. It should return 1 according to specs.Right?

\${requestScope['none'] + 1}

Dumitru Postoronca
Ranch Hand
Posts: 46
Bindu Lakhanpal wrote:So it means Tomacat 6.0.18 is not behaving as expected from specificaton.And Tomcat 5.5.9 was behaving as expected.
It means that for SCWCD exam you should use Tomcat 5.5 as the book says.

Chinmaya Chowdary
Ranch Hand
Posts: 432
Thankyou Hasnain, Ankit.

From now onwards I will read specifications. During my preparation of SCWCD, I studied only HFSJ. I might have to read specifications also inorder to get deeper sense of subject.
Bindu Lakhanpal wroteChinmaya, can you please check the output of following line in tomcat 5. I am just curious. It should return 1 according to specs.Right?

\${requestScope['none'] + 1}

I tested this in Tomcat5.5.9, it returns 1.
Dumitru Postoronca wroteIt means that for SCWCD exam you should use Tomcat 5.5 as the book says.

Yes, we should use.

Ranch Hand
Posts: 664
Wow... this is was an informative discussion!

I am getting a Jasper Exception for <% \${requestScope['none'] + 1} %> using TomCat 5.5.

Christophe Verré
Sheriff
Posts: 14691
16
Wow... this is was an informative discussion!

Yes, but you're out of subject here. You're trying to write non-java code in a scriptlet.

Ranch Hand
Posts: 664
oops.. that was an honest mistake!