How to Convert Boolean (True or False) to String Value in Groovy?
def setOnePlueOne(def user, def booleanValue) {
    updateAnswerAttribute(userRandy, ??????)
    ...
}

so what I have to do to convert that boolean to string and updateAnswerAttribute as true or false in DB.

Added getter and setter:

public Boolean setOnePlueOne() {
    return OnePlueOne;
}
public void getOnePlueOne(Boolean onePlueOne) {
    this.onePlueOne = onePlueOne;

Now, I need to convert that (Boolean onePlueOne) in string (true or false) and send it through set method to def booleanValue:

def setOnePlueOne(def user, def booleanValue) {
    updateAnswerAttribute(userRandy, ??????) 
    ...
}

this will update or create value in DB.

2 Answers

The two best ways of doing this are :

  1. String.valueOf(booleanValue)
  2. Boolean.toString(booleanValue)

Though the preferred is the first one as second gives null pointer when booleanValue = null.

Best approach to converting Boolean object to string in java

1

Use toString() method of any Boolean, e.g.:

Boolean flag = true
String value = flag.toString()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

James H. Sterling

James H. Sterling

Environmental Science & Climate Journalist

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.

Share this article
Twitter Facebook Pinterest