---
title: "Get Compare Exchange Value Operation"
url: "https://docs.ravendb.net/6.2/client-api/operations/compare-exchange/get-compare-exchange-value"
version: "6.2"
language: "java"
---

# Get Compare Exchange Value Operation

> Client language: Java. Also available for [C#](https://docs.ravendb.net/6.2/client-api/operations/compare-exchange/get-compare-exchange-value.md), [Node.js](https://docs.ravendb.net/6.2/client-api/operations/compare-exchange/get-compare-exchange-value.nodejs.md).
>
> RavenDB publishes an official agent skill for coding assistants at https://docs.ravendb.net/skills/ravendb/SKILL.md (also installable with `npx skills add ravendb/skills`). The skill describes itself as: "Read this before writing any RavenDB query, client call or /databases/ request. Covers server setup, document modelling, the Node.js, .NET and Python clients, RQL, indexes, vector search and AI agents. Code written from recall tends to run and return the wrong rows, because RQL resembles SQL and the clients take field names as plain strings."

> **Note**
>
> - Use `GetCompareExchangeValueOperation` to return the saved compare-exchange *Value* for the specified *Key*.
>
> - For an overview of the 'Compare Exchange' feature click: [Compare Exchange Overview](https://docs.ravendb.net/6.2/client-api/operations/compare-exchange/overview.java.md)
>
> - In this page:
>   - [Syntax](#syntax)
>   - [Example I - Value is 'long'](#example-i---value-is-)
>   - [Example II - Value is a custom object](#example-ii---value-is-a-custom-object)

## Syntax

**Method**:

```java
GetCompareExchangeValueOperation(Class<T> clazz, String key);
```

**Returned object**:

```java
public class CompareExchangeValue<T> {
  private String key;
  private long index;
  private T value;

  public CompareExchangeValue(String key, long index, T value) {
      this.key = key;
      this.index = index;
      this.value = value;
  }

  public String getKey() {
      return key;
  }

  public void setKey(String key) {
      this.key = key;
  }

  public long getIndex() {
      return index;
  }

  public void setIndex(long index) {
      this.index = index;
  }

  public T getValue() {
      return value;
  }

  public void setValue(T value) {
      this.value = value;
  }
}
```

| Parameter | Type | Description |
| - | - | - |
| **Key** | String | The unique object identifier |
| **Value** | `T` | The existing value that *Key* has |
| **Index** | long | The version number of the *Value* that is stored for the specified *Key* |

## Example I - Value is 'long'

```java
CompareExchangeValue<Long> readResult
  = store.operations().send(new GetCompareExchangeValueOperation<>(Long.class, "nextClientId"));

Long value = readResult.getValue();
```

## Example II - Value is a custom object

```java
CompareExchangeValue<User> readResult
  = store.operations().send(
      new GetCompareExchangeValueOperation<>(User.class, "AdminUser"));

User admin = readResult.getValue();
```

## See also

- [Overview](https://docs.ravendb.net/6.2/client-api/operations/compare-exchange/overview.java.md)
- [Get Compare-Exchange Values](https://docs.ravendb.net/6.2/client-api/operations/compare-exchange/get-compare-exchange-values.java.md)
- [Put a Compare-Exchange Value](https://docs.ravendb.net/6.2/client-api/operations/compare-exchange/delete-compare-exchange-value.java.md)
- [Cluster Transaction - Overview](https://docs.ravendb.net/6.2/client-api/session/cluster-transaction/overview.md)
- [Operations overview](https://docs.ravendb.net/6.2/client-api/operations/what-are-operations.java.md)
