Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in mock question on page no. 433 of Head First Servlets & JSP 2nd edition Chapter 8

 
Ninad Kulkarni
Ranch Hand
Posts: 802
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Pawel Nowacki
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ninad. See coersion rules . It will help.
 
Ninad Kulkarni
Ranch Hand
Posts: 802
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Pawel Nowacki & Chinmaya Chowdary.
 
Nidhi Sar
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jan Skowronski
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic