0

Which approach is better to use for performance primitive datatypes or Wrapper classes?

Hardip Patel
  • 85
  • 2
  • 11

3 Answers3

5

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


So if your need is not fulfilled by primitives then you use boxed primitives, Like in case of Collections

Sumit Singh
  • 15,743
  • 6
  • 59
  • 89
1

The choice depends on the need; whether u need primitve or an object. Wrapper classes are there to provide utility methods and also to be used easily in Collections. Depending on your need you should pick from primitives and wrapper classes. The better you chose the lesser will be overheads of autoboxing/auto-unbboxing.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
0

Generally, you should use primitive types unless you need an object for some reason (e.g. to put in a collection). Even then, consider a different approach that doesn't require a object if you want to maximize numeric performance.

Linga
  • 10,379
  • 10
  • 52
  • 104