Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: fixing code sample for compilation

...

In this noncompliant code example, constructors for class Boolean return distinct newly-instantiated objects. Using the reference equality operators in place of value comparisons will yield unexpected results.

Code Block
bgColor#FFCCCC
public void exampleEqualOperator(){

  Boolean b1 = new Boolean("true");
  Boolean b2 = new Boolean("true");

  if( b1 == b2; )           // never equal
    System.out.println("Never printed");

}

Compliant Solution (new Boolean)

In this compliant solution, the values of autoboxed Boolean variables may be compared using the reference equality operators because the Java language guarantees that the Boolean type is fully memoized. Consequently, these objects are guaranteed to be singletons.

Code Block
bgColor#CCCCFF

public void exampleEqualOperator(){

  Boolean b1 = true; // Or Boolean.True
  Boolean b2 = true; // Or Boolean.True

  if(b1 == b2;)          // always equal
    System.out.println("Will always be printed");

}

Exceptions

EXP03-EX1 In the unusual case where a program is guaranteed to execute only on a single implementation, it is permissible to depend upon implementation-specific ranges of memoized vulnerabilities.

...

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="4f736908c5c06a5e-d979a2f3-44f34757-80f58762-02b0713f2e8f3eeeb696c2df"><ac:plain-text-body><![CDATA[

[[Bloch 2009

AA. Bibliography#Bloch 09]]

4. "Searching for the One"

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="d3dfed4fe465ed91-9cdbf743-4db0490a-a00f9e21-5bd19185356ec047f4b690ea"><ac:plain-text-body><![CDATA[

[[JLS 2005

AA. Bibliography#JLS 05]]

[§5.1.7, "Boxing Conversion"

http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.1.7]

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="30220c11823b55d0-32ca3d4e-489141ee-a75287ba-a2d7e3ce55034a3099c2cb1c"><ac:plain-text-body><![CDATA[

[[Pugh 2009

AA. Bibliography#Pugh 09]]

Using == to compare objects rather than .equals

]]></ac:plain-text-body></ac:structured-macro>

...