• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

doubt in EL

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have the following question.

<%
request.setAttribute("Two","2");
Integer One = new Integer(1);
%>

${One + 1}
${Two}
${Two + 1}

What will be output of above code?

Options:
a. 1 2 3
b. 2 2 3
c. 1 1
d. 1 null 1

Please explain? Will EL performs automatic data type conversions?
 
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think output will be 123, not sure though
 
swapna rao
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Answer is : option a .It displays 123. Can anyone explain me.
 
Joshua Antony
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
${One + 1} will evaluate to (Integer)+int. since the spec is for java 1.4 automatic conversion to primitive will not take place, hence (Integer) will be ignored hence ${One + 1} = 1.

${Two} - no problem here, Two is an attribute set in request scope and has value 2 hence ${Two} = 2.

${Two + 1} will be evaluated as ${"2" + 1} and since El converts String to primitive in cases required , it will be ${2+1} and hence output as 3.
 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
d swapna, please Quote Your Sources. Thanks in advance.
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ronaldo ,

For the first case {One + 1 } can I say that , since there is no attribute bound with literal "one" , and the value of a unknown attribute in a numeric calculation evaluates to 0 . hence its {0 + 1 } which gives 1 .
Atleast this is what I could infer after having read HFS
 
swapna rao
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The above question is from the link http://www.examulator.com.

bernard,
When we use EL without specifying any scope,it starts looking in all the scopes starting with page scope.So ,in this case we have pageScope bound variable "One" and its value is 1(Integer).But the automatic conversion from Integer to int is not supported so 1(Integer) will result to "0" and ${One+1} results to 1.My understanding is , EL converts only String to required datatype and evaluates the logic. Ex:${Two+1}.Please correct me,if i'm wrong.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So ,in this case we have pageScope bound variable "One"


No, "One" is not even in pageScope. It's a local variable.

${One + 1}
There is no scoped attribute called "One", so it's null. In an addition, null is converted to 0, so the result is ${0+1}, which prints 1.

${Two}
There's a scoped attribute called "Two" in request scope, whose value is "2". In an addition, a String will be coerced to a Long (calling Long#valueOf), so "2" becomes 2, resulting in ${2}, which prints 2.

${Two + 1}
Same as before. It becomes ${2+1}, which prints 3.
 
Joshua Antony
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, even if One is declared as primitive like int one=2; there will be no change in the answer since ${One + 1} will look for One in different scopes, right ?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that's right
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic