Table of contents
Open Table of contents
Article body
Today I ran into an issue involving primitive types and their corresponding wrapper types. I will not go into their definitions here. The key point is that a wrapper is an object—yes, the familiar line that we have all heard far too often: Java is object-oriented. The fact that a wrapper is an object matters in certain situations, including the one I encountered today:
ModelAndView.setObj(entity): I was setting an entity as the object to return. One of its properties was a double. In this situation, I could not obtain the value when the property used a primitive type; I could only obtain it when I used the corresponding wrapper class.
More importantly, if a database field corresponding to a primitive such as double or int is null, assigning that value directly to the primitive causes an error. I encountered this today as well: it threw a NullPointerException. Using a wrapper type can avoid this situation.
For a more detailed explanation, see this blogger’s discussion.