> For the complete documentation index, see [llms.txt](https://www.pranaypourkar.co.in/the-programmers-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.pranaypourkar.co.in/the-programmers-guide/java/java-basics/java-data-types/primitive-types/boolean.md).

# boolean

## About

* **Definition:** `boolean` is a primitive data type in Java used to represent one of two values: `true` or `false`.
* **Usage:** It is primarily used for **conditional logic** (e.g., `if` statements, loops, and logical expressions).
* **Size:** `boolean` is **not explicitly defined** to occupy a specific number of bits in memory by the Java specification, though many implementations use **1 byte**.
* **Default Value:** The default value of `boolean` is `false`.
* **Wrapper Class:** `Boolean` is the wrapper class for `boolean` in the `java.lang` package, which allows `boolean` values to be used in collections and provides utility methods.

{% hint style="info" %}
`boolean` is a primitive and does not accept `null`. Use `Boolean` instead.
{% endhint %}

## **Characteristics**

1. **Two Possible Values Only:** The only valid values are `true` and `false`.
2. **Not Numeric:** Unlike C/C++, Java does not allow `boolean` to be treated as an integer (e.g., `true` ≠ `1` and `false` ≠ `0`).
3. **Cannot Be Cast to Other Types:** No direct or indirect casting to/from `boolean` and numeric or other types is allowed.
4. **Control Flow:** It is foundational for control flow constructs (`if`, `while`, `do-while`, `for`, etc.).
5. **Immutable:** The value of a `boolean` variable cannot be altered directly; a new assignment is needed.
6. **Logical Operations Support:** Works with logical operators like `&&`, `||`, and `!` for combining and negating conditions.
7. **Memory Usage:** Though not standardized, the JVM typically allocates **1 byte** for a `boolean` variable (smallest addressable unit of memory).
8. **Boolean Arrays:** Boolean arrays may use **1 bit per value** internally (optimized by JVM), though this varies by implementation.

## **Operations with `boolean`**

### **Logical Operators**

| **Operator** | **Name**    | **Example**     | **Result** |
| ------------ | ----------- | --------------- | ---------- |
| `&&`         | Logical AND | `true && false` | `false`    |
| \`           |             | \`              | Logical OR |
| `!`          | Logical NOT | `!true`         | `false`    |
| `^`          | Logical XOR | `true ^ false`  | `true`     |

### **Comparison Operators**

| **Operator** | **Name**   | **Example** | **Result**                          |
| ------------ | ---------- | ----------- | ----------------------------------- |
| `==`         | Equality   | `a == b`    | Compares two boolean values         |
| `!=`         | Inequality | `a != b`    | Returns true if values are unequal. |

### **Wrapper Class `Boolean`**

The `Boolean` class in `java.lang` provides utilities for working with `boolean` values.

**Key Features:**

1. **Object Representation of `boolean`:** Allows usage in Collections (e.g., `List<Boolean>`).
2. **Constants:**
   * `Boolean.TRUE`: A constant holding the `true` value.
   * `Boolean.FALSE`: A constant holding the `false` value.
3. **Static Methods:**
   * `Boolean.parseBoolean(String s)`: Parses a string as a boolean.
   * `Boolean.valueOf(String s)`: Returns a `Boolean` instance representing the string value.

## Examples

### Basic example

```java
boolean isJavaFun = true;
boolean isFishTasty = false;

// Simple If-Else
if (isJavaFun) {
    System.out.println("Java is fun!");
} else {
    System.out.println("Java is not fun.");
}

// Combining Logical Operations
if (isJavaFun && !isFishTasty) {
    System.out.println("Java is fun, but fish isn't tasty.");
}
```

### **Using `boolean` with streams**

```java
import java.util.Arrays;

public class BooleanStreamExample {
    public static void main(String[] args) {
        // Boolean array
        Boolean[] flags = {true, false, true, false};

        // Count `true` values
        long trueCount = Arrays.stream(flags)
                .filter(Boolean::booleanValue)
                .count();
        System.out.println("Number of true values: " + trueCount);
    }
}
```

### **Using `boolean` with array**

```java
public class BooleanArrayExample {
    public static void main(String[] args) {
        boolean[] flags = new boolean[5]; // Default values are false
        flags[0] = true;
        flags[3] = true;

        for (boolean flag : flags) {
            System.out.println(flag);
        }
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://www.pranaypourkar.co.in/the-programmers-guide/java/java-basics/java-data-types/primitive-types/boolean.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
