问题
In my line of work I'm mostly working on business applications that have a lot of database code. I don't usually write number crunching scientific logic.
This makes me wonder - would it be a good idea to always use wrapper types in my entity classes? I've found myself frequently converting primitives to wrappers. On the contrary I can't think of a case where I couldn't do without a primitive.
If I declared the fields in my entities to be wrappers the data read from db would be automatically mapped onto these by my persistence layer, thus saving me the trouble of having to create them manually.
Some things only come through experience hence I'm asking you folks, has anyone tried going with this approach? Are there any problems with it?
I'm not concerned about performance but am asking this from good practice/architecture point of view.
回答1:
Wrappers vs Primitives
Pros
- can be used with generics
- supports many additional methods.
Cons
- uses more cpu
- uses more memory
- implies the value can be
null
- is longer to type in some cases.
来源:https://stackoverflow.com/questions/12640826/are-there-any-disadvantages-to-wrapper-types-besides-performance