Use int or integer

There are many final values ​​in my code, for example 10000. They never change, I do nothing with them, sometimes it checks as if(number1 == number2) .

Is it possible to use int or Integer for such numbers?

+4
source share
7 answers

In your case, use int .

In general, if you are in a situation where you can use either a primitive value, it is almost always preferable:

  • They are potentially more memory efficient.
  • You can always use == reliably, which is not an Integer class type - you will need to use .equals() to make sure that you get what you want. See here for more details.

These days, memory requirements are much less, but the big problem is the second, which can cause a lot of errors if you do not know about it (especially since there are cases where == will work the way you expect, and others where it will not - this is not a "reliable" error in this sense.)

+9
source

Since Integer are objects, they take up more space in memory. If you never use Integer methods or anything else that requires it to be an object, int more memory efficient since int is a primitive type.

In the grand scheme of things, it probably does not matter how much memory modern computers have. However, the harm is not safe.

It should be noted that if you use == for Integer s, you check to see if the links match. For Integer objects, it is preferable to use the equals() method.

+5
source

use int since it is a primitive type. This is always the best option due to memory efficiency, to choose a primitive type instead of choosing a full-fledged wrapper object.

+3
source

In fact, it does not matter if it is int or Integer . In java, they are very interchangeable when using autoboxing .

However, if you plan on storing them in a collection, it matters. Collections accept only objects, which means you will need to use Integer .

If you decide to use Integer and want to compare them, do not use == . Use the compareTo method or the equals method equals .

+2
source

You are comparing links, so it is better to use int. Never use "==" to compare Integer objects (or Float, Double, Short, Character, Byte, Long, BigDecimal ...) for equality. Compare the numbers in their primitive form - i.e. Work with int, short, long, double.

If you want to compare integers, use the compareTo () method.

+2
source

Use int when you can, and use Integer when you should :

If the values ​​can be null, use Integer , but you will need to use the compareTo and Equals methods to compare between them.

If your value cannot be null, use int , so you will have more readable code with the classic operators == , >= , < , etc.

  // int primitive, easy to handle int a = 5; int b = 5; int c = null; // Compile error, can't have null primitive values if(a == b) // True if(a > b) // False if(a >= b) // True // Integer, more complicated: Integer A = new Integer(5); Integer B = new Integer(5); Integer c = null; // Work if(A == B) // False, they are not the same objects if(A.equals(B)) // True, same value if(A > B) // Compile error... if(A.compareTo(B)>0) // A > B, the int version is more readable if(A.compareTo(B)>=0) // A >= B, the int version is more readable 
+1
source

For constants use int , as for constants, you only need to get their value. Everything else that comes with Integer is redundant in this case.

If you need to pass integers as parameters, it is convenient to use Integer , since you will get another parameter of the null parameter value (i.e., the case with an invalid parameter or the legal case without a value).

+1
source

All Articles