• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Doubt in HFSJ page-420  RSS feed

 
Ranch Hand
Posts: 437
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a doubt in question 6 given in HFSJ. I am not able to understand option 'E'. Can anybody explain me.

Thanks in advance.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will be good if you can post the question with the options.
 
Padma priya Gururajan
Ranch Hand
Posts: 437
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question is:
<% java.util.List list = new java.util.ArrayList();
list.add("a");
list.add("2");
list.add("c");
request.setAttribute("list",list);
request.setAttribute("listIdx","1");
%>
<%-- insert code here --%>


Which, inserted at the line will evaluate to c?


My question is why ${list{'listIdx'+1]) is invalid?

Please reply.


Thanks in advance.
 
Reesha Ashok
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

My question is why ${list{'listIdx'+1]) is invalid?



Because 'listIdx'+1 is a String value which cannot be converted to integer. List and ArrayList does index search. EL can coerce String to integer value in certain cases which means ${list[1]} is same as ${list["1"]}. But this conversion is not possible with ${list['listIdx'+1]}.
However, Had the expression been ${list[listIdx+1]}, it would have been a valid expression.Reason being listIdx set as an attribute in Request. request.setAttribute("listIdx","1");
In this scenario, listIdx since it's not in quotes would evaluate to the value set in request which is "1" and the expression becomes ${list["1"+1]} which is same as ${list[1+1]} or ${list[2]} which is perfectly valid.

Reading pages 372 and 375 of HFSJ should give you more insight into this.
 
Padma priya Gururajan
Ranch Hand
Posts: 437
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Reesha,
How does 'listIdx'+1 become a string? Does 'listIdx' hold a value of 1?

Thanks in advance.
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Padma,
Any content within quotes will be considered as String and the same rule applies to listIdx. If you use listIdx without quotes , the variable value will be evaluated.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!