• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EL expression question

 
Abhishek Sharma
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Ranchers,

For given lines codes



Is second option is correct ?
because as per logiq for Lists and Arrays the value under [] operator is converted into integer and then handeled as per EL expression, but here we have 'list' inside [] operator that can not be converted into integer.

question belongs to HFJS
chapter scriptless jsp
question 6


Thanks in advance,
Abhishek
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't have that second option in my copy of the book.
[ February 23, 2007: Message edited by: Marc Peabody ]
 
Khaled Mahmoud
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Regarding the fist one which is :

It will not compile because the compiler knows it cannot add a String
to an integer

Regarding the second one which is :

This will cause a Runtime exception to occur.
Why the exception occurs ???
At run time,the expression list['list'] will be evaluated.Since the elements in the list can be indexed by integers only, what is inside the single quotes is expected to be an integer.
Why no compilation time occurs. Simply imagine you have a hashtable or hashmap instead of the list and its keys are strings.
 
Hunny Lee
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Will fail to compile since you should use one or the other (dot or []), and not both at the same time.
If you remove the dot, i.e. ${list['listIdx'+1]}, it will still fail, since 'listIdx' is a String.
If you remove the quotes, i.e. ${list[listIdx+1]} -> this will be evaluated to ${list[2]}, then the output is "c"


'list' (the 3rd) is String, so it will fail since you can't retrieve values from a list using a String that cannot be converted into an integer. (e.g. '1')
If you remove the quotes in the 3rd 'list', i.e. ${list[list[list]]}, it would still fail since list is an ArrayList, and can't be converted to an integer.
 
Khaled Mahmoud
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right Hunnu Lee regarding the first one
I actually didn't see the "."
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think you have a errata in the options to that question.
please check it !
 
Amirr Rafique
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to add two little points
1- Any thing in [] operator enclosed in quotes will be taken as key of Map, property of bean or index of List/Array.
2- Any thing in [] operator not enclosed in quotes will be evaluated first and then the resulting value will be used.

Hopes it help
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic