• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EL doubt

 
narendra bhattacharya
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


a It will print 1 because the statement is valid.
b It will print 2 because the statement is valid.
c It will throw an exception because a is undefined.
d It will throw an exception because the expression’s syntax is invalid.

and the ans is a..

can smbody help me in this..
also if i replace a by "a" it will give give ERROR..
 
Herman Pomstra
Greenhorn
Posts: 7
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
EL treats undefined variables as null or zero. So "5 + 3 + a" evaluates to "5 + 3 + 0". This is > 0 so 1 is printed.

Regards
Herman
 
narendra bhattacharya
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Herman Pomstra wrote:EL treats undefined variables as null or zero. So "5 + 3 + a" evaluates to "5 + 3 + 0". This is > 0 so 1 is printed.

Regards
Herman


Thanks Herman
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2419
95
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
also if i replace a by "a" it will give give ERROR..


That is correct, "a" is a String that does not represent a number and cannot be translated into a number like for instance "15".
${"Not a Number" + 3} will evaluate in a javax.servlet.jsp.el.ELException

If you use:
${(5 + 3 + a > 0) ? 1 : 2}
the EL-parser will try to find an attribute in one of the 4 scopes named 'a' and if it is not found it will be treated as an 0 or null like Herman is saying (@ Herman congratulations on your result!)

Have a look at some EL examples in my notes (ScwcdLinks)

Regards,
Frits

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic