Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding Expression Language

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

I am following Hanumant Deshmukh material for preparation of SCWCD Exam.I have small query regarding the following question.Can Anybody expalain the below question?

Given:
11. <% java.util.Map map = new java.util.HashMap();
12. request.setAttribute("map", map);
13. map.put("a", "true");
14. map.put("b", "false");
15. map.put("c", "42"); %>
Which three EL expressions are valid and evaluate to true? (Choose three.)
A. ${not map.c}
B. ${map.d or map.a}
C. ${map.a and map.d}
D. ${map.false or map.true}
E. ${map.a and map.b or map.a}
F. ${map['true'] or map['false']}
Answer: A, B, E


For the above question i can accept answer as E.but i didn't accept in case of Option B adn A.In case of Option B key 'd' is not there but they are saying it will not throw any exception.Please guide me to understand this solution.

With Regards,
Rama Krishna.Y
 
Mazer Lao Tzu
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Option B will not throw an exception because the HashMap.get method will return null if the key does not exist without throwing an exception.
 
yekkala krishna
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mazer,

Thanks for your reply.Now i understand the case in case of Option B.Please give me a correct idea in case of Option A.Here also map.c returns 42 then how it will return as true.

can you clarify my doubt.
 
Ankit Garg
Sheriff
Posts: 9579
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In option a, we have the not EL operator. So the operand will be converted (coerced) to Boolean. The rules for coercion to Boolean as given in section 2.8.5 of the JSP specification are
• If A is null or "", return false
• Otherwise, if A is a Boolean, return A
• Otherwise. if A is a String, and Boolean.valueOf(A) does not throw an exception,
return it
• Otherwise, error

So Boolean.valueOf will be applied to the value "42". The documentation of Boolean.valueOf(String) says
Returns a Boolean with a value represented by the specified string. The Boolean returned represents a true value if the string argument is not null and is equal, ignoring case, to the string "true".

So the value "42" will become false on conversion and then not will convert it to true...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic