wood burning stoves 2.0*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Confusing in EL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Confusing in EL" Watch "Confusing in EL" New topic
Author

Confusing in EL

Sherif Shehab
Ranch Hand

Joined: Mar 05, 2007
Posts: 483

Hi Guys,

Is this true or false

a- ${foo["5"]} is valid syntax if foo is a Map ?
b- ${foo[5]} is valid syntax if foo is a List or array ?

Can anyone clarify for me the difference between ${foo[" "]} & ${foo[ ]} (with " " & witout "") in all cases ?

THANKS
Sherif


Thanks,
Sherif
Michael Ku
Ranch Hand

Joined: Apr 20, 2002
Posts: 510
Ask yourself these questions:
If a have a Map, do I access one of its elements with a String or an integer?
If I have an Array or a List, do I access one of its elements with a String or an integer?

This is just basic Java knowledge that is applied the [] operator when dealing with Maps, Lists and Arrays
Mark Garland
Ranch Hand

Joined: Nov 11, 2006
Posts: 226
That's correct.
The only thing I would add is about values being evaluated in EL.
It would be wrong to suggest that the [] operator maps directly onto Java syntax.

My HFSJ is in work, but there was a explanation using the words "ambient"/"genre" if memory serves.

Something like,

${foo[ambient]} - would mean the variable ambient would be evaluated in EL
${foo["ambient"]} - would take the string literal "ambient".

HTH


28/06/06 - SCJP - 69%, 05/06/07 - SCWCD - 92%, 28/02/08 - IBM DB2 v9 Fundamentals (Exam 730) - 87%, 18/11/08 - IBM DB2 v9 DBA (Exam 731) - 89%, 26/02/11 - SCBCD - 88%
Michael Ku
Ranch Hand

Joined: Apr 20, 2002
Posts: 510
You are correct. I wanted Sherif to try and think about this himself without giving him the answer.

I find that with the EL operators you can always reason them out. This suggestion was the first step in the reasoning process. First you recognize what type of object you are referencing in the EL expression (Bean, List, Map, etc). Whatever follows needs to make sense for the type of object you are working with.

Next step is to see if you are using the . operator or the [] operator
( . operator only works with Maps and Beans)

Then, if you are using the [] operator, you can see if you have a String expression, an int or a literal. If you have a literal, then it will be evaluated, substituted and further analysis will be done to resolve the EL expression (is it a String, an int, are there any arithmetic expressions, logic operators, etc)
Sherif Shehab
Ranch Hand

Joined: Mar 05, 2007
Posts: 483

Originally posted by Michael Ku:
Ask yourself these questions:
If a have a Map, do I access one of its elements with a String or an integer?
If I have an Array or a List, do I access one of its elements with a String or an integer?

This is just basic Java knowledge that is applied the [] operator when dealing with Maps, Lists and Arrays


hi Michael ok , in Map case we can access the elemnets with String so do you mean in EL we will use ? , in Array or list case we use integer so in EL we will use? Is this Right?
Mark Garland
Ranch Hand

Joined: Nov 11, 2006
Posts: 226
No.
EL is very forgiving, so you can use things that you would not normally be able to 'get away with' in Java.

${foo["ambient"]}
This means 'Use the String literal "ambient"'. So, you know you must be working with a Map, because Lists/Arrays work with numerical indexes.

${foo[ambient]}
This means 'Evaluate the variable ambient', and then use it with the collection.
If ambient were a variable that had the value 2, then you could be working with an array/list. If the variable had the value "car", then you could be working with a Map.

I said EL is forgiving as you can get away with the below...

Lets assume foo is a List (so numerical indexes)
${foo[2]}
${foo["2"]}
Will give you the same result, as the String "2" is converted for you.


I know this wont be helpful if you haven't got it, but the HFSJ mini chapter on EL is pretty good and should tell you everything you should need to know for the exam.
Charle's book tells you how it works under the scenes in a little more depth if that's more your taste.

Finally, I am sure there are some good resources on the net - anyone?

MG
Michael Ku
Ranch Hand

Joined: Apr 20, 2002
Posts: 510
Mark, very nice explanation.

HFSJ does the best job of explaining these concepts. I just passed the SCWCP exam and used HFSJ almost exclusively.

When working with the [] operator in the EL, you have to learn to reason it out. You cannot memorize this, you have to think.

Try to think out some examples. Create some examples by yourself and test your ability to think it thru.
Sherif Shehab
Ranch Hand

Joined: Mar 05, 2007
Posts: 483

Thanks guys for your replies

Mark Garland
Ranch Hand

Joined: Nov 11, 2006
Posts: 226
You're welcome.

Were they helpful?
If so, prove it by answering the question you originally posed (that way it would act as a reference for others).
Michael Ku
Ranch Hand

Joined: Apr 20, 2002
Posts: 510
mark, nice call
Sherif Shehab
Ranch Hand

Joined: Mar 05, 2007
Posts: 483

Originally posted by Mark Garland:
You're welcome.

Were they helpful?
If so, prove it by answering the question you originally posed (that way it would act as a reference for others).


so the answers now are : both true . Right? :roll:
by the way i wanna ask the difficulty of the exam because i saw some tough questions in enthuware simulator in tough section really if i faced some of these questions in the exams for sure i'm a dead man !! because they are very tough & tricky , so do you think i may face many of them in the exam? & do you advice me to keep in the easy & very easy sections? (questions were categorized like this very easy , easy , tough ,real brainer)
Michael Ku
Ranch Hand

Joined: Apr 20, 2002
Posts: 510
My advice is to learn how to reason the question out. Learn the rules of how they are evaluated. If you do, it will not matter if the question is easy or hard. You will be able to answer any question correctly
Mark Garland
Ranch Hand

Joined: Nov 11, 2006
Posts: 226
I agree - both true.

Did you have the HFSJ book? If so, I'd *definitely* recommend reading that section - the authors believe that it covers everything you should need for EL (and it's only about 10pages long).
Sherif Shehab
Ranch Hand

Joined: Mar 05, 2007
Posts: 483

ya i got it, ok i'll read it
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confusing in EL
 
Similar Threads
HFSJ 2nd edition - wrong answer marked as correct
hfs chap-8 qno-7 doubt
Question in SCWCD mock exams to be discussed
HFSJ 1st edition, help me understand this