Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7076857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:20:58+00:00 2026-05-28T06:20:58+00:00

In groovy: println ‘test’ as Boolean //true println ‘test’.toBoolean() //false println new Boolean(‘test’) //false

  • 0

In groovy:

println 'test' as Boolean //true
println 'test'.toBoolean() //false
println new Boolean('test') //false

Can anyone clarify this behavior?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:20:59+00:00Added an answer on May 28, 2026 at 6:20 am

    Both of these

    println 'test'.toBoolean() //false
    println new Boolean('test') //false
    

    instantiate a java.lang.Boolean using the constructor that takes a single String argument. According to the javadocs, the rule is:

    Allocates a Boolean object representing the value true if the string argument is not null and is equal, ignoring case, to the string “true”. Otherwise, allocate a Boolean object representing the value false.

    In both of the cases above, the String does not match ‘true’ (case-insensitively), so the Boolean created is false.

    By contrast 'test' as Boolean follows the Groovy language rules for coercion to a boolean, which allows you to write:

    if ('hello') {
        println 'this string is truthy'
    }
    

    For a String, the rule is that if it’s empty or null, it evaluates to false, otherwise true.

    I agree that this could be considered a bit inconsistent, but given a choice between consistency with the constuctor of java.lang.Boolean and utility, I think they were right to choose the latter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

like this: Groovy: map = ['a':1,'b':2] doubler = this.&doubleMethod map.each(doubler) println map What's the
File1.groovy def method() { println test } File2.groovy method() I want to load/include the
In Groovy Console I have this: import groovy.util.* import org.codehaus.groovy.runtime.* def gse = new
The following Groovy code: 1.upTo(5) {println it} Produces this compilation error: No signature of
Groovy has a concept of GStrings. I can write code like this: def greeting
In grails, how can I add some code to the UrlMappings.groovy (ex: println) so
The following groovy script doesn't compile import java.util.concurrent.Callable println b; Callable<String> callable = new
Say I have src/test/groovy/MyTest.groovy: class MyTest extends GroovyTestCase { void testDummy() { println 'DUMMY'
I've written a small Groovy script that exposes a very weird behaviour. can anyone
Groovy adds the execute method to String to make executing shells fairly easy; println

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.