• 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 ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Regarding Expression Language

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

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?

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
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.
Posts: 9671
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • 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...
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic