ColdFusion true is not a Java boolean

None of these ColdFusion constructs strictly equate to a true-valued Java boolean (or Boolean):

true
"true"
1
YES

But this does:

JavaCast("java.lang.Boolean", true);

Makes me wonder what ColdFusion’s literal true (no quotes) actually is. Probably just a string.

Edit prompted by Todd:
Definitely just a string. And definitely not a boolean. Also not particularly suprising but something I’d never thought about is the fact that ColdFusion “is” is not the same as Java “equals”.

  1. <cfscript>
  2. a = true;
  3. b = CreateObject("java","java.lang.Boolean").init("true");
  4. c = "true";
  5. </cfscript>
  6.  
  7. <cfoutput>
  8.         a = true;<br>
  9.         b = CreateObject("java","java.lang.Boolean").init("true");<br>
  10.         c = "true";<br>
  11.         <br>
  12.         <cfdump var="#a.getClass().getName()#"><br>
  13.         <br>
  14.         b.equals(a) #b.equals(a)#<br>
  15.         a.equals(b) #a.equals(b)#<br>
  16.         b.equals(JavaCast("boolean",a)) #b.equals(JavaCast("boolean",a))#<br>
  17.         b is a #b is a#<br>
  18.         a is b #a is b#<br>
  19.         <br>
  20.         a.equals(c) #a.equals(c)#<br>
  21.         c.equals(a) #c.equals(a)#<br>
  22.         a is c #a is c#<br>
  23.         c is a #c is a#<br>
  24.         <br>
  25.         b.equals(c) #b.equals(c)#<br>
  26.         c.equals(b) #c.equals(b)#<br>
  27.         b is c #b is c#<br>
  28.         c is b #c is b#<br>
  29. </cfoutput>

gives this:

a = true;
b = CreateObject("java","java.lang.Boolean").init("true");
c = "true";

java.lang.String

b.equals(a) NO
a.equals(b) NO
b.equals(JavaCast("boolean",a)) YES
b is a YES
a is b YES

a.equals(c) YES
c.equals(a) YES
a is c YES
c is a YES

b.equals(c) NO
c.equals(b) NO
b is c YES
c is b YES

3 Comments

  1. Todd Sharp says:

    Try this:
    Cfset a = true
    cfdump a.getclass().getname()

  2. admin says:

    Good idea. Post edited.

  3. ColdFusion handles boolean evaluation in a similar fashion as JavaScript/ECMAScript does.

    Probably the biggest difference is that JS will treat any string with a positive length as true and any empty string as false. This means the string “true” and “false” both evaluate as true ince the strings have a positive length.

Leave a Reply