Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!

# EL value for unknown variables. Math and logical.

Cristiano Sganzerla
Ranch Hand
Posts: 44
I was working with EL to see the different behaviors when dealing with math and logical operators.

HFSJ stated:

Logical operators, unknown variable receives 'false'
Math operators, unknown variable receives 0

The first problem
How is this evaluated, since foo isnt known
Example:

\${foo < 3} ---> false < 3 ??? What the hell is that ?

\${!foo} ---> true = ok

\${foo eq false} ---> false = why ???
\${foo == false} ---> false = why ???

\${'0' || '0'} ---> false - is it ok to use logical operators with string? Always evaluate to false ?

\${0 || 0} ---> exception - Attempt to coerce a value of type "java.lang.Long" to Boolean

\${'0' == 0} ---> true - Converts '0' to numeric.

I stated that if one of the variables is numerical, the other variable will be cast to numerical, even if it is a String, then the math or logical operator will be applyed.

I really dont know if these stupid questions will be done in the exam, since we are not talking about SCJP, but i was wondering how EL really works with these variations.

Carl Wauters
Greenhorn
Posts: 19
- Considering your first problem: \${foo eq aValue} This will only return true when you compare foo to null.
- Considering your second problem: \${'0' || '0'} This will only return true when one of those strings is equal to 'true'

About casting: i'm not completely sure, but I guess you can compare Strings to Strings, numbers to numbers, booleans to booleans, booleans to Strings and numbers to Strings, but in the latter case, the String must be able to be converted to a number.

Hope this helps...

Regards,

Carl

Cristiano Sganzerla
Ranch Hand
Posts: 44
Thanks Carl !!!