Using int vs Integer

后端 未结 9 1444
抹茶落季
抹茶落季 2020-12-01 01:28

I came across a class using Integer variables to capture size to be used in a for loop. Is this good practice or should we use the int primitive data type?

I         


        
相关标签:
9条回答
  • 2020-12-01 01:39

    There can be a performance penalty due to boxing and unboxing, as you incur the overhead of converting between a primitive and a reference type.

    However, the Integer class adds additional methods that can be very useful.

    0 讨论(0)
  • 2020-12-01 01:45

    This is a potential disaster waiting to happen in large projects. The coder here forgot that each Integer is actually a new object, and, to use it as an int, there has to be boxing and unboxing all the time. Not only is it inefficient, it's also not going to run as expected. You are best advised to always use int where possible, and only use Integer for placing these values into lists, containers or for database storage. Remember, comparing Objects using >, < and == means something else than when you are using the same operators to compare primitives.

    0 讨论(0)
  • 2020-12-01 01:48

    Dont use it only to loop. Use the primitive type int (better performance), which is not the same as the Class Integer.

    The Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field whose type is int.

    In addition, this class provides several methods for converting an int to a String and a String to an int, as well as other constants and methods useful when dealing with an int.

    So, use Integer if you need to call the Integer functions or need to assign a null value to it.

    You will also need to use Integer instead of int for generics like List

    0 讨论(0)
  • 2020-12-01 01:49

    I normally use Integer because of purely OO. Performance of int is definitely far better than its object counterpart but it is noticeable only when you are looping millions time in the loop.

    IMHO, If performance is paramount importance in you application and you want to squeeze even single nano second then use int without any brain. But if OO and readability is main concerned then use Integer.

    0 讨论(0)
  • 2020-12-01 01:59

    When there is a need of using objects, you have to use the Wrapper classes, like Integer, Double, Float, etc...

    eg:

     int n = Integer.parseInt("10");
    

    Here we are converting the string to an integer (Primitive type) , but method parseInt(String str) works only on Wrapper classes (ie Object), so we used it... you will find many more use of it in java.

    0 讨论(0)
  • 2020-12-01 02:00

    the Integer class is provided so that values can be boxed/unboxed in a pure OO manner. use int where appropriate unless you specifically need to use it in an OO way; in which case Integer is appropriate.

    Java Int vs Integer

    However, very different things are going on under the covers here. An int is a number; an > Integer is a pointer that can reference an object that contains a number.

    ...

    An int is not an object and cannot passed to any method that requires objects. A common case is in using the provided collection classes ( List , Map , Set ) - though it is possible to write versions of these classes that provide similar capabilities to the object versions. The wrapper classes ( Integer , Double , etc) are frequently required whenever introspection is used (such as in the reflection API).

    A better description of when to use one vs. the other:

    Choosing between int and Integer

    I'll start with how these types should be used before going into detail on why.

    • Prefer int for performance reasons
    • Methods that take objects (including generic types like List<T>) will implicitly require the use of Integer
    • Use of Integer is relatively cheap for low values (-128 to 127) because of interning - use Integer.valueOf(int) and not new Integer(int)
    • Do not use == or != with Integer types
    • Consider using Integer when you need to represent the absence of a value (null)
    • Beware unboxing Integer values to int with null values
    0 讨论(0)
提交回复
热议问题