1

From Effective Java - Joshua Bloch

Item 49- Prefer primitive types to boxed primitives

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! Autoboxing reduces the verbosity, but not the danger, of using boxed primitives.

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,

I prefer to write the code like:

public double someCalculation(double primitiveDouble, double anotherPrimitiveDouble) {
        return primitiveDouble + anotherPrimitiveDouble;
}

instead of :

public double someCalculation(Double doubleObject, double doublePrimitive) {
    if (doubleObject!= null) {
        return doubleObject+ doublePrimitive;
    }
    return 0d;
}

In the caller, I do the validation for null and the call of doubleValue() in the boxed primitive doubleObject like:

public void someCaller() {
    double doublePrimitive = getSomeDoublePrimitive();    
    Double doubleObject = getSomeDoubleObject();

     if (doubleObject != null) {
            double result = someCalculation(doubleObject.doubleValue(), doublePrimitive)
    } 
 .....
}

instead of:

public void someCaller() {
    double doublePrimitive = getSomeDoublePrimitive();    
    Double doubleObject = getSomeDoubleObject();

     if (doubleObject != null) {
            double result = someCalculation(doubleObject, doublePrimitive)
    } 
 .....
}

When I call the method someCalculation(double primitiveDouble, double anotherPrimitiveDouble) I use the call doubleValue() in the Double parameter doubleObject as doubleObject.doubleValue() to explicitly unbox the double primitive value of doubleObject.

By this way:

  • I'm sure that my method get a primitive double and not a Double object.

  • 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.

  • I believe that I improve readability of my code because I'm more explicit of my intention.

  • I pass the accurate parameter to the method someCalculation(double primitiveDouble, double anotherPrimitiveDouble)that only need primitive types

My Questions:

What are the advantages/disadvantages of using explicit unboxing ?

It is acceptable to use doubleObject.doubleValue() when I call some method that only accept primitive types parameters as someCalculation(double primitiveDouble, double anotherPrimitiveDouble)?

Jesus Zavarce
  • 1,729
  • 1
  • 17
  • 27
  • Is your question whether it's better to do `someCalculation(doubleObject, doublePrimitive)` instead of `someCalculation(doubleObject.doubleValue(), doublePrimitive)`? Or about the method signatures? A much more concise, targeted question would be clearer. – T.J. Crowder Aug 16 '15 at 09:00
  • 3
    Since your method takes a primitive as argument, it will always receive a primitive as argument. The call to doubleValue() is unnecessary: the compiler will add it for you if you don't do it explicitely. So it's a matter of opinion: some people don't like redundant code, like relying on auto-unboxing and are experienced enough to know what actually happens. Some people prefer making it clear that an unboxing happens, that could lead to a NullPointerException if the Double variable is null. Choose your camp. – JB Nizet Aug 16 '15 at 09:08
  • @JBNizet , Your comment is a good candidate to answer my question. – Jesus Zavarce Aug 16 '15 at 14:19
  • 1
    Then consider it answered. Your question is on hold because it's opinion-based. And it probably will stay as is forever. Because it *is* opinion-based, as my comment explains. Even if I wanted to post that comment as an answer, I could not, because when a questionis on hold, it can't be answered anymore. – JB Nizet Aug 16 '15 at 14:21
  • StackOverflows says that users with 3000 reputation can cast up to 50 close votes per day. When a question reaches 5 close votes, it is marked [on hold], and will no longer accept answers. Those users may vote to reopen questions the same way. Each user may only vote to close and reopen a given question once. Moderators may close or reopen any question with a single vote. In my case I have 5 votes that put my question as opinion-based before my edition. One of these votes come from you. – Jesus Zavarce Aug 16 '15 at 14:30
  • If is not possible to ask one opinion about one topic in stackOverflow, we are blocking the opportunity to read opinions that could change your own opinion . – Jesus Zavarce Aug 16 '15 at 14:45
  • @JBNizet your answer for me is a good answer for my question, why we have to block the possibility of expression of others? the possibility to know the point of view of many people about a topic. I'm new in stack Overflow, I think that is very important listen the others and define our own judgment. – Jesus Zavarce Aug 16 '15 at 14:58
  • StackOverflow won't change its rules just for you. You may argue all you want whether it's good or bad to accept opinion-based questions, the fact is that StackOverflow has decided not to accept them. – JB Nizet Aug 16 '15 at 15:02
  • Ok good to know this point. – Jesus Zavarce Aug 16 '15 at 15:04
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/87096/discussion-between-jesus-zavarce-and-jb-nizet). – Jesus Zavarce Aug 16 '15 at 15:27

1 Answers1

2

You should prefer to use primitive types in your case as ultimately caller need primitive types. Also wrapper objects have more overhead than their primitive counterparts (memory & boxing) when they are not actually required.

So in your example, if you go ahead will object, extra memory is allocated for object, extra operation is performed for auto boxing and then one more operation is performed to convert object to primitive data type, which you can avoid with simple use of primitive type at the very first place.

Have a glance at When to use wrapper class and primitive type , it will provide your more details.

Community
  • 1
  • 1
M Sach
  • 33,416
  • 76
  • 221
  • 314