• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

EL Question from HFSJ

 
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question is from HFSJ in chapter 8

public class MyBean{
private java.util.Map params;
private java.util.List objects;
priavte String name;

public java.util.Map getParams() { return params;}
public String getName() { return name;}
public java.util.List getObjects() { return objects;}

}

which will cause errors(assume that an attribute names mybean can be found, and its type is MyBean)? (choose all that apply)

A.${mybean.name}
B.${mybean["name"]}
C.${mybean.objects.a}
D.${mybean["params"].a}
E.${mybean.params["a"]}
F.${mybean["objects"].a}

My answer is CDEF

but the book answer is CF

Please help it understanding this question.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question is about the difference between accessing elements of a List vs. accessing elements of a Map in EL.

The answer is C & F and they both fail for the same reason. There is no method called "a" for a List object and you cannot access element "a" of a list so an error will occur because the system will not be able to access it.

The reason that D & E do not create errors is that when you use try to access "a" for a map, it will look for an element with the key of "a". If no element with the key of "a" is found it will just return null, which does not cause an error in EL.
 
Amruth Puppala
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Awesome explanation Craig Suchanec
Really thank you so much ..I got very clarity now.
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what about the fact that . dot operator can not be used with the list and array. In both D & F, . is being used for accessing something from the list.
 
author
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Amit Prakash Singh:
what about the fact that . dot operator can not be used with the list and array. In both D & F, . is being used for accessing something from the list.



Actually, in option D the dot operator is being used on a Map.

In option F, the dot operator is being used on a List; and as Craig mentioned, this is why option F is one of the correct answers ("it will cause an error").

Make sense?

Cheers,
Bryan
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks , i had also same doubt , now i am bit fine now but still has some doubt.

I undertood it like (Reason for CF being wrong) :
Its not about the non-existance of "a" as method in List object (as said in this post), its about wrong usage of dot operater as its not allowed to use dot operater with list as book says for example it was bit the same case as if i am trying to access {musicList.1}

Please correct me otherwise.

[sorry for reinitiating the thread ]

Thanks
Vishal Chugh
 
    Bookmark Topic Watch Topic
  • New Topic