Object to boolean android

Boolean Class in JAVA with Example

Boolean Class is a wrapper class that wraps the primitive type boolean. This class provides mechanism to convert primitive type “boolean” to object type “Boolean” or vice versa.

Object of Class Boolean can contain only single value whose type is boolean.

This class extends Object class and implements Serializable and Comparable interfaces.

Table of Contents

Boolean Class Constructors

1. Boolean (boolean value)

This constructor creates a Boolean object with value which is of type boolean that can be true or false

2. Boolean (String string)

This constructor creates a Boolean object with value “true” if and only if value of string passed is “true” ignoring case or else object is initialized with “false”.

Boolean Methods

1. boolean booleanValue()

This method returns primitive value of the Boolean object.

Let us discuss this method with the help of example as shown below

Output:

Description:

Above program has been divided into 3 steps that we will discuss one by one

  • In Step 1, Boolean class object is created and value “true” is assigned to it
  • In Step 2, primitive type of boolean is created
  • In Step 3, value of Boolean object is assigned to primitive type boolean using booleanValue() method.

2. int compareTo(Boolean b)

This method as the name suggests compares one Boolean object with other. This method returns int value which can be negative, positive or zero.

Zero is returned when the two objects have same values.

Positive value is returned when calling object has value “true” and argument object has value “false”.

Negative value is returned when calling object has value “false” and argument object has value “true”.

Let us discuss this method with the help of example as shown below

Output:

Description:

Above program has been divided into 3 steps that we will discuss one by one

  • In Step 1, Boolean class objects are created.
  • In Step 2, Values are assigned to the objects created in step 1.
  • In Step 3, Boolean objects are compared using method compareTo()

3. boolean equals(Object obj)

This method returns boolean value true or false, true when the both the Boolean objects have same value, or else false.

Let us discuss this method with the help of example as shown below

Output:

Description:

Above program has been divided into 3 steps that we will discuss one by one

  • In Step 1, Boolean class objects are created and values are assigned to them.
  • In Step 2, boolean flag1 and flag2 are created.
  • In Step 3, objects are compared using equals() method and boolean values are stored in the flag.

4. static boolean getBoolean(String name)

This method is to check boolean value of System property. This method will return true if the system property named by the argument exists and has value ”true” assigned to it or else this method will return false.

Читайте также:  Ускоритель для системы андроид

Let us discuss this method with the help of example as shown below

Output:

Description:

Above program has been divided into 3 steps that we will discuss one by one

  • In Step 1, boolean primitives are created.
  • In Step 2, Values for two system properties are assigned using setProperty () method.
  • In Step 3, System properties are assigned to boolean primitive types using getBoolean() method.

5. int hashCode()

This method returns a hash code for the calling Boolean object.

Let us discuss this method with the help of example as shown below

Output:

Description:

Above program has been divided into 2 steps that we will discuss one by one

  • In Step 1, Boolean objects are created and values are assigned to them.
  • In Step 2, Hash codes are obtained using hashCode() method.

6. static boolean parseBoolean(String s)

This method parses the value of string arguments, and will return true if and only if string value is “true” (ignoring case) or else it will return false.

Let us discuss this method with the help of example as shown below

Output:

Description:

Above program has been divided into 3 steps that we will discuss one by one

  • In Step 1, two strings are created and values are assigned to them.
  • In Step 2, boolean types are created.
  • In Step 3, Values are assigned to primitive types using parseBoolean() method.

7. String toString()

This method returns a String object which represents the value of calling Boolean object. If the value of Boolean object is null then this method will return false.

Let us discuss this method with the help of example as shown below

Output:

Description:

Above program has been divided into 3 steps that we will discuss one by one

  • In Step 1, Boolean class objects are created
  • In Step 2, Values are assigned to objects created in Step 1.
  • In Step 3, String values of Boolean objects are assigned to String objects using toString() method

8. static String toString(boolean b)

This method returns a String object which represents the value of calling boolean primitive type. This method is called directly from the Boolean class as this method is static.

Let us discuss this method with the help of example as shown below

Output:

Description:

Above program has been divided into 3 steps that we will discuss one by one

  • In Step 1, boolean primitive types are created.
  • In Step 2, Values are assigned to primitive types created in Step 1.
  • In Step 3, String values of boolean primitives are assigned to String objects using toString() method.

9. static Boolean valueOf(boolean b)

This method returns an object of Boolean class that represents the specified boolean value.

Let us discuss this method with the help of example as shown below

Output:

Description:

Above program has been divided into 3 steps that we will discuss one by one

  • In Step 1, Boolean objects are created.
  • In Step 2, boolean primitives are created and values are assigned to them
  • In Step 3, Values of primitive types are assigned to Boolean objects

10. static Boolean valueOf(String s)

This method returns Boolean object with a value true or false

true when the value of string is “true” (ignoring case)

false when the value is any string other than “true”.

Читайте также:  Car crash для андроид

Let us discuss this method with the help of example as shown below

Output:

Description:

Above program has been divided into 3 steps that we will discuss one by one

  • In Step 1, Boolean objects are created.
  • In Step 2, String objects are created and values are assigned to them
  • In Step 3, boolean value of Strings are assigned to Boolean objects

Источник

Utility method to convert Boolean into boolean and handle null in Java

Is there a utility method in Java which converts Boolean into boolean and automatically handles null reference to Boolean as false?

7 Answers 7

? That’s a single expression, which will only evaluate to true if value is non-null and a true-representing Boolean reference.

On java 8 you can do:

You can also do:

Are you looking for a ready-made utility ? Then I think Commons-Lang BooleanUtils is the answer. It has a method toBoolean(Boolean bool).

I don’t know whether it exists or not. I’d write a one liner like:

This would be a method you could write that would do the trick. This would return false if the Boolean is null.

If you’re golfing, an explicit null check followed by automatic unboxing is shorter than the canonical answer.

This will handle both null and Boolean value

Not the answer you’re looking for? Browse other questions tagged java boolean utility-method or ask your own question.

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.12.3.40888

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

How to convert String object to Boolean Object?

How to convert String object to Boolean object?

14 Answers 14

Try (depending on what result type you want):

  • Boolean: this does not create new instances of Boolean, so performance is better (and less garbage-collection). It reuses the two instances of either Boolean.TRUE or Boolean.FALSE .
  • boolean: no instance is needed, you use the primitive type.

The official documentation is in the Javadoc.

Autoboxing could also be used, but it has a performance cost.
I suggest to use it only when you would have to cast yourself, not when the cast is avoidable.

You have to be carefull when using Boolean.valueOf(string) or Boolean.parseBoolean(string). The reason for this is that the methods will always return false if the String is not equal to «true» (the case is ignored).

Because of that behaviour I would recommend to add some mechanism to ensure that the string which should be translated to a Boolean follows a specified format.

The value of b is true if the string is not a null and equal to true (ignoring case).

Beside the excellent answer of KLE, we can also make something more flexible:

(inspired by zlajo’s answer. :-))

Well, as now in Jan, 2018, the best way for this is to use apache’s BooleanUtils.toBoolean .

This will convert any boolean like string to boolean, e.g. Y, yes, true, N, no, false, etc.

Result:

My way to convert string to boolean.

This is how I did it:

For my case is mostly either 1 or true. I use hashes as dividers.

To get the boolean value of a String, try this:

Читайте также:  Как поставить android kitkat

If there is an error, it will return null. Example:

Why not use a regular expression ?

We created soyuz-to library to simplify this problem (convert X to Y). It’s just a set of SO answers for similar questions. This might be strange to use the library for such a simple problem, but it really helps in a lot of similar cases.

Please check it — it’s very simple and has a lot of other useful features

This will give you an idea of what to do.

This is what I get from the Java documentation:

parseBoolean

public static boolean parseBoolean(String s)

Parses the string argument as a boolean. The boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string » true «.

Parameters:

s — the String containing the boolean representation to be parsed

Returns: the boolean represented by the string argument

Источник

How to read/write a boolean when implementing the Parcelable interface?

I’m trying to make an ArrayList Parcelable in order to pass to an activity a list of custom object. I start writing a myObjectList class which extends ArrayList and implement Parcelable .

Some attributes of MyObject are boolean but Parcel don’t have any method read/writeBoolean .

What is the best way to handle this?

13 Answers 13

Here’s how I’d do it.

You could also make use of the writeValue method. In my opinion that’s the most straightforward solution.

Afterwards you can easily retrieve it with a simple cast to Boolean :

Under the hood the Android Framework will handle it as an integer:

you declare like this

boolean type needs to be converted to something that Parcel supports and so we can convert it to int.

AndroidStudio (using 2.3 atm), after implementing Parcelable on your class, you can simply hold your mouse pointer over your class name and it asks you to add the parcelable implementation:

From the four fields, it generates the following:

I normally have them in an array and call writeBooleanArray and readBooleanArray

If it’s a single boolean you need to pack, you could do this:

Short and simple implementation in Kotlin, with nullable support:

Add methods to Parcel

I suggested you the easiest way to implement Parcelable if you are using Android Studio.

Simply go to File->Settings->Plugins->Browse Repository and search for parcelable .See image

It will automatically create Parcelable.

And there is a webiste also for doing this. http://www.parcelabler.com/

You could pack your boolean values into a byte using masking and shifting. That would be the most efficient way to do it and is probably what they would expect you to do.

It is hard to identify the real question here. I guess it is how to deal with booleans when implementing the Parcelable interface.

Some attributes of MyObject are boolean but Parcel don’t have any method read/writeBoolean.

You will have to either store the value as a string or as a byte. If you go for a string then you’ll have to use the static method of the String class called valueOf() to parse the boolean value. It isn’t as effective as saving it in a byte tough.

If you go for a byte you’ll have to implement a conversion logic yourself.

When unmarshalling the Parcel object you have to take care of the conversion to the original type.

Источник

Оцените статью