user1451415
user1451415

Reputation: 107

Use int or Integer

In my code there are a lot of final values, like 10000. They never change, i do nothing with them, just sometimes checks like if(number1 == number2).

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

Upvotes: 4

Views: 2669

Answers (7)

Radu Murzea
Radu Murzea

Reputation: 10920

It doesn't really matter if it's int or Integer. In java, they're very interchangeable by the use of autoboxing.

It does however make a difference if you plan to store them in a collection. Collections only accept objects, which means you'll have to use Integer.

If you decide to use Integer and want to compare them, don't use ==. Instead, use the compareTo method or the equals method.

Upvotes: 2

Mesop
Mesop

Reputation: 5263

Use int when you can and use Integer when you must:

If the values can be null use Integer but you will have to use the methods compareTo and Equals to make comparison between then.

If your value can't be null, useint, that way you will have a more readable code with the classical 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

Upvotes: 1

maksimov
maksimov

Reputation: 5811

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

Where you need to pass integers as parameters, it's handy to use Integer as you get one more option of the parameter value being null (i.e. a case with an invalid parameter, or a legit no-value case).

Upvotes: 1

Michael Berry
Michael Berry

Reputation: 72399

In your case, use int.

In a general case, if you're in the situation where you could feasibly use either the primitive value is almost always preferred:

  • They're potentially more memory efficient
  • You can always use == reliably, which isn't the case with the class Integer type - you'd have to use .equals() to ensure you were getting what you wanted. See here for more details.

Memory requirements are a lot less of an issue these days, but the big gotcha is the second one, which can cause the potential for a lot of bugs if you're not aware of it (especially since there's cases where == will work as you expect, and others where it won't - it's not a "reliable" bug in that sense.)

Upvotes: 8

Milos
Milos

Reputation: 81

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

If you want to compare Integers use method compareTo().

Upvotes: 2

SimplyPanda
SimplyPanda

Reputation: 725

Since Integers are objects, they take up more space in memory. If you don't ever use Integer methods or anything else requiring it to be an object, ints are more memory-efficient, since int is a primitive type.

In the grand scheme of things, it probably won't matter given how much memory modern computers have. No harm being safe though.

I should note that if you're using == for Integers, you're checking if the references are the same. For Integer objects, the equals() method will be preferred.

Upvotes: 5

Chandra Sekhar
Chandra Sekhar

Reputation: 19500

use int, as it is the primitive type. It is always a better option because of memory efficiency to choose a primitive type instead of choosing a full fledged wrapper object.

Upvotes: 3

Related Questions