Arpitha Mamidala
Arpitha Mamidala

Reputation: 225

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.

Upvotes: 16

Views: 48505

Answers (2)

upadhyayRakes
upadhyayRakes

Reputation: 152

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

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

Upvotes: 6

Rahul Babu
Rahul Babu

Reputation: 790

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

Upvotes: 21

Related Questions