Two Laptop Bag*
The moose likes JSP and the fly likes EL: coercing of null values and empty strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "EL: coercing of null values and empty strings" Watch "EL: coercing of null values and empty strings" New topic
Author

EL: coercing of null values and empty strings

Roland Mueller
Greenhorn

Joined: Oct 28, 2011
Posts: 7

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

Joined: Jan 10, 2002
Posts: 60053
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Roland Mueller
Greenhorn

Joined: Oct 28, 2011
Posts: 7

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
Bartender

Joined: Oct 14, 2005
Posts: 18127
    
    8

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

Joined: Oct 28, 2011
Posts: 7

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: ""
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: EL: coercing of null values and empty strings
 
Similar Threads
Unable to use El in custom tags
Problem While using EL
Request Attribute confusion
EL relational operators and empty string ""
<c:forEach> to iterate a collection