wood burning stoves 2.0*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Doubt in HFSJ page-420 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Doubt in HFSJ page-420" Watch "Doubt in HFSJ page-420" New topic
Author

Doubt in HFSJ page-420

Padma priya Gururajan
Ranch Hand

Joined: Oct 05, 2006
Posts: 435
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.


Padma priya N.G.
Be the change you want to be - Mahatma Gandhi
Reesha Ashok
Ranch Hand

Joined: Apr 05, 2008
Posts: 32
Will be good if you can post the question with the options.


Thanks & Regards,<br />Reesha
Padma priya Gururajan
Ranch Hand

Joined: Oct 05, 2006
Posts: 435
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

Joined: Apr 05, 2008
Posts: 32
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

Joined: Oct 05, 2006
Posts: 435
Hi Reesha,
How does 'listIdx'+1 become a string? Does 'listIdx' hold a value of 1?

Thanks in advance.
swapna rao
Ranch Hand

Joined: Feb 12, 2008
Posts: 53
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.


SCJP-88<br />SCWCD-94
 
Consider Paul's rocket mass heater.
 
subject: Doubt in HFSJ page-420