• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EL: coercing of null values and empty strings

 
Roland Mueller
Greenhorn
Posts: 7
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am bit confused about the corecing behaviour of EL. Using Glassfish 3.1.2 (EL 2.2.4) I got empty strings coreced to 0 but null is not. Thus, the expressions using null values return false.

Nevertheless, in EL lang spec 2.2 section 1.18.3 the following can be found:
If A is null or "", return 0.


Shouldn't all 4 expressions below return true when the coerced values of "" and null are compared with -1 ?

The code example is an extension from the following old thread: http://www.coderanch.com/t/526307/JSP/java/EL-relational-operators-empty-string



Thanks!
Regards,
Roland
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64717
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
null is not a value -- it is the absence of a value. null is not equal to 0, it is not equal to anything. The only thing you can logically do with null is to test for it or not. The empty operator exists for that purpose.
 
Roland Mueller
Greenhorn
Posts: 7
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question I have is not about whether accepting null or empty string is reasonable. Instead there is some specified behaviour in the EL spec (quoted above) and the suspicion that Glassfish does not follow it.

I stumbled over this when preparing for the Servlet&Jsp exam where one has to deal with such lawyer-like issues. In practice - where common sence should rule - I agree with you about null and the same applies also IMHO to the empty string used in numeric expressions. I am missing here the clearness of Java, EL feels a bit like Basic ...



 
Paul Clapham
Sheriff
Posts: 20980
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's true that 1.18.3 does say what you posted. But you omitted to say why that section would apply. Instead, if you read section 1.18.1 (the section about relational operators) you'll see this:

If A is null or B is null, return false


Which means that the coercion described in 1.18.3 doesn't apply in this case.

(Warning: I'm quoting from the 2.1 spec, not the 2.2 spec, so check your version to make sure it agrees with what I said.)
 
Roland Mueller
Greenhorn
Posts: 7
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, thank you! That's the answer! In the example above for null the rule I overlooked applies.

A (hopefully) valid example where not only "" but also null is used as number should be some numeric operation. Surprise, surprise :hunf: here there is a different behaviour of Glassfish3 and Tomcat7 when handling "": Glassfish throws an exception:




"" + 1 = Exception: java.lang.NumberFormatException: For input string: ""
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic