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

前端 未结 7 720
醉梦人生
醉梦人生 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:47

    Long is the Object form of long, and Integer is the object form of int.

    The long uses 64 bits. The int uses 32 bits, and so can only hold numbers up to ±2 billion (-231 to +231-1).

    You should use long and int, except where you need to make use of methods inherited from Object, such as hashcode. Java.util.collections methods usually use the boxed (Object-wrapped) versions, because they need to work for any Object, and a primitive type, like int or long, is not an Object.

    Another difference is that long and int are pass-by-value, whereas Long and Integer are pass-by-reference value, like all non-primitive Java types. So if it were possible to modify a Long or Integer (it's not, they're immutable without using JNI code), there would be another reason to use one over the other.

    A final difference is that a Long or Integer could be null.

    0 讨论(0)
  • 2020-12-12 12:47

    a) object Class "Long" versus primitive type "long". (At least in Java)

    b) There are different (even unclear) memory-sizes of the primitive types:

    Java - all clear: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

    • byte, char .. 1B .. 8b
    • short int .. 2B .. 16b
    • int .. .. .. .. 4B .. 32b
    • long int .. 8B .. 64b

    C .. just mess: https://en.wikipedia.org/wiki/C_data_types

    • short .. .. 16b
    • int .. .. .. 16b ... wtf?!?!
    • long .. .. 32b
    • long long .. 64b .. mess! :-/
    0 讨论(0)
  • 2020-12-12 12:52

    When it comes to using a very long number that may exceed 32 bits to represent, you may use long to make sure that you'll not have strange behavior.

    From Java 5 you can use in-boxing and out-boxing features to make the use of int and Integer completely the same. It means that you can do :

    int myInt = new Integer(11);
    Integer myInt2 = myInt;
    

    The in and out boxing allow you to switch between int and Integer without any additional conversion (same for Long,Double,Short too)

    You may use int all the time, but Integer contains some helper methods that can help you to do some complex operations with integers (such as Integer.parseInt(String) )

    0 讨论(0)
  • 2020-12-12 12:54

    There are a couple of things you can't do with a primitive type:

    • Have a null value
    • synchronize on them
    • Use them as type parameter for a generic class, and related to that:
    • Pass them to an API that works with Objects

    Unless you need any of those, you should prefer primitive types, since they require less memory.

    0 讨论(0)
  • 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<Integer>, for example (you can't use int in a List directly).

    0 讨论(0)
  • 2020-12-12 12:58
    • By default use an int, when holding numbers.
    • If the range of int is too small, use a long
    • If the range of long is too small, use BigInteger
    • If you need to handle your numbers as object (for example when putting them into a Collection, handling null, ...) use Integer/Long instead
    0 讨论(0)
提交回复
热议问题