title: Java Best Practices: Using doubleValue() in calls that only accept primitive types parameters
body: <p>From <a href="http://www.oracle.com/technetwork/java/effectivejava-136174.html" rel="nofollow">Effective Java - Joshua Bloch</a></p>
  
  <p>Item 49- Prefer primitive types to boxed primitives</p>
  
  <blockquote>
    <p>Use primitives in preference to boxed primitives whenever you have
    choice. Primitive types are simple and faster. If you must use boxed
    primitives, be careful! <strong>Autoboxing reduces the verbosity, but not the
    danger, of using boxed primitives</strong>.</p>
  </blockquote>
  
  <p>When I need a method that always return a primitive value that is calculate with their parameters, and their parameters could come from a Double object, </p>
  
  <p><strong>I prefer to write the code like:</strong></p>
  
  <pre><code>public double someCalculation(double primitiveDouble, double anotherPrimitiveDouble) {
          return primitiveDouble + anotherPrimitiveDouble;
  }
  </code></pre>
  
  <p><strong>instead of :</strong></p>
  
  <pre><code>public double someCalculation(Double doubleObject, double doublePrimitive) {
      if (doubleObject!= null) {
          return doubleObject+ doublePrimitive;
      }
      return 0d;
  }
  </code></pre>
  
  <p>In the caller, I do the validation for null and the call of <code>doubleValue()</code> in the  boxed primitive <code>doubleObject</code> like:</p>
  
  <pre><code>public void someCaller() {
      double doublePrimitive = getSomeDoublePrimitive();    
      Double doubleObject = getSomeDoubleObject();
  
       if (doubleObject != null) {
              double result = someCalculation(doubleObject.doubleValue(), doublePrimitive)
      } 
   .....
  }
  </code></pre>
  
  <p><strong>instead of:</strong> </p>
  
  <pre><code>public void someCaller() {
      double doublePrimitive = getSomeDoublePrimitive();    
      Double doubleObject = getSomeDoubleObject();
  
       if (doubleObject != null) {
              double result = someCalculation(doubleObject, doublePrimitive)
      } 
   .....
  }
  </code></pre>
  
  <p>When I call the method <code>someCalculation(double primitiveDouble, double anotherPrimitiveDouble)</code> I use the call <code>doubleValue()</code> in the Double parameter <code>doubleObject</code> as <code>doubleObject.doubleValue()</code> to explicitly unbox the double primitive value of <code>doubleObject</code>. </p>
  
  <p>By this way:</p>
  
  <ul>
  <li><p>I'm sure that my method get a primitive double and not a Double object. </p></li>
  <li><p>If the null validation disappears in the caller, I go to be more closer to the place of an eventually NullPointerException in my stack trace.</p></li>
  <li><p>I believe that I improve readability of my code because I'm more explicit of my intention.</p></li>
  <li><p>I pass the accurate parameter to the method <code>someCalculation(double primitiveDouble, double anotherPrimitiveDouble)</code>that only need primitive types</p></li>
  </ul>
  
  <p><strong>My Questions:</strong></p>
  
  <p>What are the advantages/disadvantages of using explicit unboxing ?</p>
  
  <p>It is acceptable to use <code>doubleObject.doubleValue()</code> when I call some method that only accept primitive types parameters as   <code>someCalculation(double primitiveDouble, double anotherPrimitiveDouble)</code>?</p>