jQuery in Action, 3rd edition
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes about EL in HFSJ Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "about EL in HFSJ " Watch "about EL in HFSJ " New topic

about EL in HFSJ

dahooo chang

Joined: Dec 20, 2005
Posts: 1
in HFSJ page 377:

why would compiler translate ${musicList[ nums[0] + 1] } into
${musicList["2"]} instead of
I thought String concatenation would be of higher priority than primitive addition.
[ December 20, 2005: Message edited by: dahooo chang ]
Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

You should have a look at the spec : JSP. Binary operators - A {+,-,*} B
If A or B is BigInteger, coerce both to BigInteger and then:
If operator is +, return A.add( B )
Otherwise coerce both A and B to Long and apply operator

The String will be converted first, so the concatenation will not occur.

Btw, you meant ${musicList["2"]}, right ?
[ December 20, 2005: Message edited by: Satou kurinosuke ]

[My Blog]
All roads lead to JavaRanch
Charles Lyons
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
Originally posted by dahooo chang:

I thought String concatenation would be of higher priority than primitive addition.

Can I just point out that EL does not overload the + operator for string concatenation; EL interprets + only as the numeric addition operator. So, operator precedence with + really shouldn't be an issue. I've never read HF, but I can't believe they would have missed the fact that + cannot be used for string concatenation in EL... It's one of those intuitive Java things which backfires on you!

It's interesting that the Apache JEXL project (http://jakarta.apache.org/commons/jexl/) incorporates the overloading of +, but it isn't a compatible implementation of EL as defined in JSP 2.0!

To do concatenation in compliant JSP 2.0 containers (or in JSTL 1.0 tag attributes), you should use two EL adjacent expressions. For example, to concatenate 'myvar' to 'myvar2' you would do:

and not:

which is illegal unless we want to perform numerical addition on the numbers 'myvar' and 'myvar2'.

Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
I agree. Here's the link: http://aspose.com/file-tools
subject: about EL in HFSJ
It's not a secret anymore!