| Author |
Doubt in mock question on page no. 433 of Head First Servlets & JSP 2nd edition Chapter 8
|
Ninad Kulkarni
Ranch Hand
Joined: Aug 31, 2007
Posts: 780
|
|
Hi All,
I have a doubt in mock question given on page no. 433 of Head First Servlets & JSP 2nd edition Chapter 8 scriptless JSP
11. <% java.util.List list = new java.util.ArrayList();
12. list.add(“a”);
13. list.add(“2”);
14. list.add(“c”);
15. request.setAttribute(“list”, list);
16. request.setAttribute(“listIdx”, “1”);
17. %>
18. <%-- insert code here --%>
Options are
A. ${list.2}
B. ${list[2]}
C. ${list.listIdx+1}
D. ${list[listIdx+1]}
E. ${list[‘listIdx’ + 1]} <-------------- Option E is incorrect because EL tries to coerce 'listIdx' to a Long which is invalid
F. ${list[list[listIdx]]}
Correct options are B, D & F
The book given reason indicated above why option E is incorrect but I have a doubt that how EL tries to coerce 'listIdx' to Long? where Long comes from? here after + sign there is 1 integer then how EL tries to coerce Long?
|
SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
|
 |
Pawel Nowacki
Ranch Hand
Joined: Nov 14, 2008
Posts: 67
|
|
There's JSP.2.3.5.1 Binary operators - A {+,-,*}B chapter in JSP specification that states:
- If A and B are null, return (Long) 0
- If A or B is a BigDecimal, coerce both to BigDecimal and then:
- If operator is +, return A.add( B )
(...)
- Otherwise coerce both A and B to Long and apply operator
- If operator results in exception, error
I think those two last apply here.
|
 |
Chinmaya Chowdary
Ranch Hand
Joined: Apr 21, 2008
Posts: 432
|
|
|
Hi Ninad. See coersion rules . It will help.
|
 |
Ninad Kulkarni
Ranch Hand
Joined: Aug 31, 2007
Posts: 780
|
|
|
Thank you very much Pawel Nowacki & Chinmaya Chowdary.
|
 |
Nidhi Sar
Ranch Hand
Joined: Oct 19, 2009
Posts: 252
|
|
Reviving an old thread since I have exactly the same doubt.
The coersion page that a poster points to, has the following regarding + operator:
As I understand it, since none of the if conditions are met by "listIdx",
- the logic would fall to "otherwise" :
- listIdx will be coerced to a long
- operator will be applied. long_with_value_1 + 1 will give a long_with_value_2 & list[long] will fail.
So here are my questions:
1. Why does list[long] fail?
2. What about option D? listIdx is still an object of the type string. Just because it does not have quotes around it, why wouldn't the same thing happen to it?
Thanks for any clarifications.
|
"A problem well stated is a problem half solved.” - Charles F. Kettering
SCJP 6, OCPJWCD
|
 |
Jan Skowronski
Greenhorn
Joined: Dec 21, 2007
Posts: 4
|
|
Ok, regarding last post:
1. list[long] would not fail as such,but "listIdx" String cannot be coerced to Long, there would be an exception at parsing time.
2. listIdx is a String = "2" and that value of String is parsed niceley, becoming Long=2 so it can be used.
Hope that helps, I'm having a go at OCPJWCD on Wednesday (5th of Jan), and I failed the mock exam from Head First book;)
|
 |
 |
|
|
subject: Doubt in mock question on page no. 433 of Head First Servlets & JSP 2nd edition Chapter 8
|
|
|