Long vs Integer, long vs int, what to use and when?

前端 未结 7 734
醉梦人生
醉梦人生 2020-12-12 12:14

Sometimes I see API\'s using long or Long or int or Integer, and I can\'t figure how the decision is made for that?

7条回答
  •  抹茶落季
    2020-12-12 12:57

    An int is a 32-bit integer; a long is a 64-bit integer. Which one to use depends on how large the numbers are that you expect to work with.

    int and long are primitive types, while Integer and Long are objects. Primitive types are more efficient, but sometimes you need to use objects; for example, Java's collection classes can only work with objects, so if you need a list of integers you have to make it a List, for example (you can't use int in a List directly).

提交回复
热议问题