| Author |
EL Coercion question
|
Yusuf Hussain
Greenhorn
Joined: Mar 07, 2006
Posts: 3
|
|
Hi all, I have a question regarding EL and primitive coercion while using the [] operator. In HFSJ chapter 8 question 6, how comes the answer F is correct and not answer e? given from HFSJ: list.add("a"); list.add("2"); list.add("c"); request.setAttribute("list", list); request.setAttribute("listIdx","1"); does ${list[list['listIdx']]} simplify to ${list[list["1"]]} which in turn simplifies to ${list["2"]} which further simplifies to ${list[2]}? what then does ${list['listIdx'+1]} simplify to? what about ${list["1"+1]}? I've read the spec but I can't seem to figure out the general rule for coercion? Thanks in advance!
|
 |
Chandra Sagi
Ranch Hand
Joined: May 05, 2005
Posts: 162
|
|
I think the result of ${list["1"+1]} would be ${list["11"]} and so could not be evaluated. Correct me if I am wrong. Thanks Chandu
|
 |
Frederic Esnault
Ranch Hand
Joined: Feb 13, 2006
Posts: 284
|
|
list["1"+1] is list[2], as EL tries to coerce String values to numeric values when possible. Pay attention to ELs like : ${v + 1.0}where v is a int variable set to 1, because EL will coerce to Double. Hence, result will be 2.0.
|
SCJP 5 - SCWCD 1.4 - SCBCD 1.3 - Certification study documents/resources: http://esnault.frederic.free.fr/certification
|
 |
Yusuf Hussain
Greenhorn
Joined: Mar 07, 2006
Posts: 3
|
|
Thank you both! But if: list["1"+1] is list[2] AND ${list[list['listIdx']]} => ${list[list[1]]} => ${list[2]} as shown in my original post Why wouldn't ${list['listIdx'+1]} coerce 'listIdx' to it's int value and then make it ${list[1+1]} => ${list[2]}? Thanks.
|
 |
Chandra Sagi
Ranch Hand
Joined: May 05, 2005
Posts: 162
|
|
Hi Frederic
list["1"+1] is list[2], as EL tries to coerce String values to numeric values when possible.
I agree with you that EL coerces String values to numeric values when possible, but the value of "1"+1 is "11". This is the case of + operator, adding int to a String returns String. I think here list["11"] is coerced to list[11] as you said. I may be wrong too. Thanks Chandu
|
 |
Frederic Esnault
Ranch Hand
Joined: Feb 13, 2006
Posts: 284
|
|
Hi again, I'm sorry to disappoint you, but "1"+1 is indeed 2. To demonstrate it, i made a simple jsp with the code fragment : The result is :
EL evaluation : 2 EL list index evaluation : C
I admit the spec is not excessivley clear, but it decribes what it does quite well. EDIT : I was planning to make a document about EL also, mixing HFSJ and spec content (spec content is much more detailed about EL, especially about coercion (well it's a spec you'll say...)), so seing people seem to hurt on this kind of subject, maybe I should do it. maybe one document including core jstl, standard actions and EL all together [ March 07, 2006: Message edited by: Frederic Esnault ]
|
 |
Ramasubbhu Allur Kuppusamy
Ranch Hand
Joined: Sep 16, 2005
Posts: 120
|
|
Hi Frederic, I appreciate your fascination in preparing documents. Without a doubt, it helps us a lot. Your answers to questions in the forum are also to the point. I am just curious to know what makes you enthusiastic to go about doing this. Are you a trainer by profession?
|
Regards,<br />Ram.<br />SCJP 1.4
|
 |
Narendra Dhande
Ranch Hand
Joined: Dec 04, 2004
Posts: 950
|
|
Hi, I think there is no + operator in EL which concat the strings. The + Operator in EL only work on numeric values.If either side of the operator is String it try to corec to numeric value. Any corrections please. Thanks
|
Narendra Dhande
SCJP 1.4,SCWCD 1.4, SCBCD 5.0, SCDJWS 5.0, SCEA 5.0
|
 |
Frederic Esnault
Ranch Hand
Joined: Feb 13, 2006
Posts: 284
|
|
Hi Ram, thanks for your comments. Well, actually I'm a trainer by profession, even it's definitely not my main work assignment, especially for the last 2 years. As for preparing documents, my main goal is to prepare myself the best way, and the "reading, understanding, writing synthetic docs" is one excellent way to burn things clearly into memory. After, i share it with you because it may help people to have these docs. Answering forum questions helps also a lot to go deep into specific details, and see situations one wouldn't think about otherwise.
|
 |
georgy jacob
Ranch Hand
Joined: Nov 23, 2005
Posts: 53
|
|
I think the operator for concatenation in EL is completely different EL doesnt have '+' for String concatenation. '+' means add in EL if u want to concatentate two strings in EL then u have to use VAR3 = ${var1)${var2}
|
 |
MInu
Ranch Hand
Joined: Oct 09, 2003
Posts: 517
|
|
does ${list[list['listIdx']]} simplify to ${list[list["1"]]} which in turn simplifies to ${list["2"]} which further simplifies to ${list[2]}? how ${list[list['listIdx']]} simplifies to ${list[list["1"]]} here ${list[list['listIdx']]} cause error. bcoz 'listIdx' is a string .... will it coerced to integer??? can anyone explain??? Thanks...
|
God Gave Me Nothing I Wanted<br />He Gave Me Everything I Needed<br /> - Swami Vivekananda
|
 |
Frederic Esnault
Ranch Hand
Joined: Feb 13, 2006
Posts: 284
|
|
Hi, No, ${list[list['listIdx']]} does not simplifies to ${list[list[2]]} When [] content is surrounded by quotes (['...']), then it is NOT evaluated, but taken as a literal. Here the literal does not coerce to a string and so an excpetion is thrown (EL list index evaluation). If you remove the quotes, then the content is evaluated as a variable. If it holds a value, it works. If the var does not exist, then null is returned which is handled by EL as an empty string.
|
 |
 |
|
|
subject: EL Coercion question
|
|
|