• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HFSJ - Q15 on page 431

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you please explain why C and F are right answers in this question. I can't really understand the explanation given. What is "a"??



Which will cause errors:

A) ${myBean.name}
B) ${myBean["name"]}
C) ${myBean.objects.a}
D) $myBean["params"].a}
E) ${myBean.params["a"]}
F) ${mybean["objects"].a}
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question wants to stress about the difference between accessing a Map and a List in EL.
"a" is variable which does not exist. When using a Map, ${myBean["params"].a}, won't because it will search for the key "a" in the map "params", calling containsKey() and get().
However, you can do this with a List only using integers.
Here are the details from the spec:

- expr-a.identifier-b is equivalent to expr-a["identifier-b"];
- Evaluate expr-a into value-a
- Evaluate expr-b into value-b
- If value-a is a Map, List, or array:
- If value-a is a Map:
If !value-a.containsKey(value-b) then return null.
Otherwise, return value-a.get(value-b)
- If value-a is a List or array:
- Coerce value-b to int (using coercion rules)
If coercion couldn't be performed: error

"a" is not an int, so error is produced.
 
snakes are really good at eating slugs. And you wouldn't think it, but so are tiny ads:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic