The product compares object references instead of the contents of the objects themselves, preventing it from detecting equivalent objects.
View on MITREFor 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.
This weakness can lead to erroneous results that can cause unexpected application behaviors.
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.
No detection method information available for this CWE.
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.
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:
String str1 = new String("Hello");String str2 = new String("Hello");if (str1 == str2) {System.out.println("str1 == str2");}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.
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:
if (str1.equals(str2)) {System.out.println("str1 equals str2");}In the following Java example, two BankAccount objects are compared in the isSameAccount method using the == operator.
Using the == operator to compare objects may produce incorrect or deceptive results by comparing object references rather than values. The equals() method should be used to ensure correct results or objects should contain a member variable that uniquely identifies the object.
public boolean isSameAccount(BankAccount accountA, BankAccount accountB) {return accountA == accountB;}In the following Java example, two BankAccount objects are compared in the isSameAccount method using the == operator.
Using the == operator to compare objects may produce incorrect or deceptive results by comparing object references rather than values. The equals() method should be used to ensure correct results or objects should contain a member variable that uniquely identifies the object.
public boolean isSameAccount(BankAccount accountA, BankAccount accountB) {return accountA.equals(accountB);}CWE-595: Comparison of Object References Instead of Object Contents is a Common Weakness Enumeration (CWE) entry maintained by MITRE. 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.
If exploited, CWE-595 (Comparison of Object References Instead of Object Contents) it can compromise Other, leading to outcomes such as Varies by Context.
Recommended mitigations for CWE-595 include: 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.
CWE-595 commonly affects Java, JavaScript, PHP and Not Language-Specific. Note that weaknesses are often language-agnostic patterns, so secure coding practices apply broadly.
A CWE (Common Weakness Enumeration) like CWE-595 describes a category of software weakness — the underlying flaw type. A CVE (Common Vulnerabilities and Exposures) identifies a specific, real-world vulnerability in a particular product. In short, a CWE is the kind of mistake, and a CVE is an instance of that mistake being found in software.