---
title: "CWE-595: Comparison of Object References Instead of Object Contents"
url: https://radicalnotion.ai/cwe/cwe-595
type: cwe
description: "The product compares object references instead of the contents of the objects themselves, preventing it from detecting equivalent objects."
updated: 2026-11-10
source: RadicalNotion.AI
license: CC BY-NC 4.0 (https://creativecommons.org/licenses/by-nc/4.0/)
---

# CWE-595: Comparison of Object References Instead of Object Contents

- **Abstraction:** Variant
- **Structure:** Simple
- **Status:** Incomplete
- **CVEs with this as primary weakness:** 0
- **Of those, in CISA KEV:** 0
- **New in 2026:** 0
- **CWE catalog version:** 4.21

## Description

The product compares object references instead of the contents of the objects themselves, preventing it from detecting equivalent objects.

For example, in Java, comparing objects using == usually produces deceptive results, since the == operator compares object references rather than values; often, this means that using == for strings is actually comparing the strings' references, not their values.

## Modes of introduction

| Phase | Note |
| --- | --- |
| Implementation | - |

## Applicable platforms

**Languages:** Java (Undetermined), JavaScript (Undetermined), PHP (Undetermined), Python (Undetermined)

## Common consequences

| Scope | Impact | Note |
| --- | --- | --- |
| Other | Varies by Context | This weakness can lead to erroneous results that can cause unexpected application behaviors. |

## Potential mitigations

### Implementation

In Java, use the equals() method to compare objects instead of the == operator. If using ==, it is important for performance reasons that your objects are created by a static factory, not by a constructor.

## Detection methods

### Automated Static Analysis

Automated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without having to execute it. Typically, this is done by building a model of data flow and control flow, then searching for potentially-vulnerable patterns that connect "sources" (origins of input) with "sinks" (destinations where the data interacts with external components, a lower layer such as the OS, etc.)

Effectiveness: High

## Demonstrative examples

### Example 1

In the example below, two Java String objects are declared and initialized with the same string values. An if statement is used to determine if the strings are equivalent.

Bad code:

```java
String str1 = new String("Hello");
String str2 = new String("Hello");
if (str1 == str2) {
    System.out.println("str1 == str2");
}
```

Good code:

```java
if (str1.equals(str2)) {
    System.out.println("str1 equals str2");
}
```

However, the if statement will not be executed as the strings are compared using the "==" operator. For Java objects, such as String objects, the "==" operator compares object references, not object values. While the two String objects above contain the same string values, they refer to different object references, so the System.out.println statement will not be executed. To compare object values, the previous code could be modified to use the equals method:

### Example 2

In the following Java example, two BankAccount objects are compared in the isSameAccount method using the == operator.

Bad code:

```java
public boolean isSameAccount(BankAccount accountA, BankAccount accountB) {
    return accountA == accountB;
}
```

Good code:

```java
public boolean isSameAccount(BankAccount accountA, BankAccount accountB) {
    return accountA.equals(accountB);
}
```

## Related weaknesses

| Relationship | CWE | Name |
| --- | --- | --- |
| Parent | [CWE-1025](https://radicalnotion.ai/cwe/cwe-1025) | Comparison Using Wrong Factors |
| Child | [CWE-597](https://radicalnotion.ai/cwe/cwe-597) | Use of Wrong Operator in String Comparison |
| Child | [CWE-1097](https://radicalnotion.ai/cwe/cwe-1097) | Persistent Storable Data Element without Associated Comparison Control Element |

## Taxonomy mappings

| Taxonomy | Entry | Id | Fit |
| --- | --- | --- | --- |
| The CERT Oracle Secure Coding Standard for Java (2011) | Use the two-argument Arrays.equals() method to compare the contents of arrays | EXP02-J | - |
| The CERT Oracle Secure Coding Standard for Java (2011) | Use the two-argument Arrays.equals() method to compare the contents of arrays | EXP02-J | - |
| The CERT Oracle Secure Coding Standard for Java (2011) | Do not use the equality operators when comparing values of boxed primitives | EXP03-J | - |
| OpenSSF Secure Coding Guide for Python | Implement Comparisons by Value Rather Than Reference | pyscg-0033 | - |

## Frequently asked questions

### What is CWE-595?

The product compares object references instead of the contents of the objects themselves, preventing it from detecting equivalent objects.

### How do you prevent CWE-595?

In Java, use the equals() method to compare objects instead of the == operator. If using ==, it is important for performance reasons that your objects are created by a static factory, not by a constructor.

### How is CWE-595 detected?

Automated Static Analysis: Automated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without having to execute it. Typically, this is done by building a model of data flow and control flow, then searching for potentially-vulnerable patterns that connect "sources" (origins of input) with "sinks" (destinations where the data interacts with external components, a lower layer such as the OS, etc.)

### What are the consequences of CWE-595?

Exploiting CWE-595 can lead to: Varies by Context.

Primary source: [MITRE CWE](https://cwe.mitre.org/data/definitions/595.html)

---

Source: [RadicalNotion.AI](https://radicalnotion.ai/cwe/cwe-595). Licensed CC BY-NC 4.0; attribute RadicalNotion.AI and link the source page when you reuse this data.
