== Why is this an issue?

Boxing is the process of putting a primitive value into a wrapper object, such as creating an `Integer` to hold an `int` value.
Unboxing is the process of retrieving the primitive value from such an object.
Since the original value is unchanged during boxing and unboxing, there is no point in doing either when not needed.

Instead, you should rely on Java's implicit boxing/unboxing to convert from the primitive type to the wrapper type and vice versa, for better readability.


=== Noncompliant code example

[source,java,diff-id=1,diff-type=noncompliant]
----
public void examinePrimitiveInt(int a) {
  //... 
}

public void examineBoxedInteger(Integer a) {
  // ...
}

public void func() {
  int primitiveInt = 0;
  Integer boxedInt = Integer.valueOf(0);
  double d = 1.0;

  int dIntValue = Double.valueOf(d).intValue(); // Noncompliant; should be replaced with a simple cast

  examinePrimitiveInt(boxedInt.intValue()); // Noncompliant; unnecessary unboxing
  examinePrimitiveInt(Integer.valueOf(primitiveInt));  // Noncompliant; boxed int will be auto-unboxed

  examineBoxedInteger(Integer.valueOf(primitiveInt)); // Noncompliant; unnecessary boxing
  examineBoxedInteger(boxedInt.intValue()); // Noncompliant; unboxed int will be autoboxed
}
----


=== Compliant solution

[source,java,diff-id=1,diff-type=compliant]
----
public void examinePrimitiveInt(int a) {
  //... 
}

public void examineBoxedInteger(Integer a) {
  // ...
}

public void func() {
  int primitiveInt = 0;
  Integer boxedInt = Integer.valueOf(0);
  double d = 1.0;

  int dIntValue = (int) d;

  examinePrimitiveInt(primitiveInt);
  examinePrimitiveInt(boxedInt);

  examineBoxedInteger(primitiveInt);
  examineBoxedInteger(boxedInt);
}
----



ifdef::env-github,rspecator-view[]

'''
== Implementation Specification
(visible only on this page)

=== Message

Remove the [un]boxing of "xxx".

'''
== Comments And Links
(visible only on this page)

=== on 21 Nov 2024, 16:48:00 Alban Auzeill wrote:
[test-code-support-investigation-for-java] Decision for scope: Main -> All. May be problems with earlier versions of java.

=== on 15 Oct 2014, 22:14:41 Freddy Mallet wrote:
I would remove the tag 'bug' on this rule because this doesn't impact the behavior at execution time.

endif::env-github,rspecator-view[]
