Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EL Coercion question

 
Yusuf Hussain
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Yusuf Hussain
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Frederic Esnault
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 517
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Frederic Esnault
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic