-2

I am new in Java/Groovy and I would like to ask when and why I should use long or Long data type.

I am asking this question, because I need to write unit tests for functions where there are types long and Long, and I am trying to understand why they are setting like this.

Ectoras
  • 1,297
  • 3
  • 13
  • 33
  • Is your question why use long vs some other type (eg int), or when you should use long instead of Long and vice versa? – user1675642 Jan 21 '16 at 13:58
  • @user1675642 "why I should use long or Long data type" – Andy Turner Jan 21 '16 at 13:59
  • I know that long is a primitive and Long is an object. My question is in which case I should use it better. Because when I write unit tests for functions I can not test null input for a long. – Ectoras Jan 21 '16 at 14:03
  • Effective Java 2nd Ed Item 49 deals with this question in detail (although other items in the book touch on the question too). – Andy Turner Jan 21 '16 at 14:04
  • "Because when I write unit tests for functions I can not test null input for a long" Doesn't this hint as to one reason to choose one over the other: if you choose `long`, you don't need to test passing in `null` because you simply can't. The type system is saving you work. – Andy Turner Jan 21 '16 at 14:11

1 Answers1

0

If you do not need to use any of the functions or other features of an Object (Long/Double), there is no point using them as they introduce additional overhead - creating objects is more expensive to your system than creating primitives. While this might seem like premature optimization, it is a very easy change to make.

As Andy pointed out you can use them with generics whereas you can't use primitives. I meant this under functions but it is worth pointing out.

Will
  • 810
  • 6
  • 21