> For the complete documentation index, see [llms.txt](https://docs.xplatform.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.xplatform.com/talon/developing-applications/authoring-user-code/message-processing/processing-messages/handling-messages/coding-for-zero-garbage/xstrings.md).

# XStrings

XStrings provide a zero garbage alternative to Java Strings for ultra-low-latency applications. XStrings are mutable, pooled string-like objects that avoid allocations during message processing.

## Overview

An XString is a zero garbage, mutable string-like object defined in ADM that can be used in place of Java Strings. Unlike Java Strings, which are immutable and allocated on the heap, XStrings are mutable and can be pooled and reused across message processing cycles. XStrings are particularly useful for fields that need to be frequently updated with string values, such as timestamps, identifiers, or status messages.

## Declaring XStrings in ADM

XStrings are declared in ADM message or state definitions using the `xstring` type:

```xml
<message name="Order">
  <xstring name="orderId"/>
  <xstring name="symbol"/>
  <int name="quantity"/>
</message>
```

## Working with XStrings

The ADM code generator produces accessor methods for XString fields that enable zero-garbage operations:

| Method                           | Description                                                |
| -------------------------------- | ---------------------------------------------------------- |
| `getXXX()`                       | Returns the XString object for reading                     |
| `setXXX(CharSequence)`           | Copies the provided CharSequence into the XString          |
| `setXXX(CharSequence, int, int)` | Copies a substring into the XString                        |
| `lendXXX()`                      | Returns a reference to the XString for direct manipulation |

Example usage:

```java
@EventHandler
public void onNewOrder(NewOrder order) {
    // Set from a String
    order.setOrderId("ORD-12345");

    // Read the value
    XString orderId = order.getOrderId();
    String orderIdStr = orderId.toString(); // creates a new String

    // Better: compare without allocating
    if (orderId.equals("ORD-12345")) {
        // process
    }

    // Direct manipulation for zero-garbage
    XString symbolXStr = order.lendSymbol();
    symbolXStr.clear();
    symbolXStr.append("AAPL");
}
```

## XString Preallocation

To achieve zero-garbage operation with XStrings, ensure they are preallocated with sufficient capacity:

```xml
<message name="Order">
  <xstring name="orderId" capacity="32"/>
  <xstring name="symbol" capacity="8"/>
</message>
```

When an XString's capacity is exceeded during a `setXXX()` call, the backing storage will be reallocated, causing garbage. Monitor your XString usage to ensure capacities are appropriately sized.

## See Also

* [Coding for Zero Garbage](/talon/developing-applications/authoring-user-code/message-processing/processing-messages/handling-messages/coding-for-zero-garbage.md) - Overview of zero garbage techniques
* [Embedded Entities](/talon/developing-applications/authoring-user-code/message-processing/processing-messages/handling-messages/coding-for-zero-garbage/embedded-entities.md) - Lifecycle-managed objects for complex messages
