This week's book giveaway is in the Java 9 forum.
We're giving away four copies of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications and have Sander Mak & Paul Bakker on-line!
See this thread for details.
Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

EL evaluation clarification  RSS feed

 
Chetan Raju
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ranchers,

I require a clarification on EL expressions. Taking examples of EL expressions as below

Case 1
------
a. myMap["mapKey"]
b. myMap[mapKey]

Case 2
------
a. myList["1"]
b. myList[1]

In Case 1 (a) above for a Map, the container does not evaluate "mapKey" but searches the map for a key similar to "mapKey".

In Case 1 (b) above, the container evaluates the mapKey. The container tries to find an attribute named mapKey and substitutes the value of this attribute in the EL expression and then correspondingly finds the key in the Map.

However, in Case 2(both a and b) above for a List, the container finds the item at the specified index in both cases whether index is specified as String literal ["1"] or otherwise [1].

Just want to know whether my understanding is right. If it is, then why is it that in Case 1 and Case 2 are handled differently i.e., the container doesn't evaluate the thing in the braces [] for Case 2 ?

Appreciate your clarification on this.
Thanks
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ust want to know whether my understanding is right. If it is, then why is it that in Case 1 and Case 2 are handled differently i.e., the container doesn't evaluate the thing in the braces [] for Case 2 ?

It also evaluates "the thing" in braces, and coerces it into an int.
So,
a. myList["1"]
b. myList[1]
c. mylist[index] is also correct if index refers to an integer.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!