Which one to use, int or Integer

I need to create a data transfer object that I will use to store records retrieved from the database. In this data transfer object, I need to declare a numeric field. For which is better - int or Integer

If I define the field as an integer, will there be any performance impact due to the Integer type, if I am going to extract more than 2000 records from the database ??

Thanks in advance.

+53
java performance types database primitive
Jan 08 '09 at 9:38
source share
11 answers

Integer is a better option since it can handle null ; for int , null will become 0 , silently if resultSet.getInt(..) . Otherwise, this may cause some kind of exception, for example: "Cannot set null to the property of the primitive."

Performance is not particularly important here.

  • if you select int , you will add additional processing code; and it wonโ€™t do you much good. Your code will not be clean and direct, thereโ€™s a lot of boiler room code, and you wonโ€™t even get performance.
  • Let me clarify that for databases, null is not null. Sometimes you enter 0 , where null intended. Imagine a case where a user has submitted a form and does not give any value to int . By default, you will get 0 . Does this make sense or really when this field is not null in the database.
+111
Jan 08 '09 at 9:44
source share

You must really make a decision based on what you need for your object to work, not the cost of performance. A performance-based solution must be implemented as soon as the speed problem has been identified with the profiler - the root of all evil and all that.

Take a look at some of the possibilities of both and use this for your solution, for example

  • Integer may be null , int cannot. So int in DB a Nullable ?
  • Do you need access to the methods of the Integer class?
  • Are you doing arithmetic?

Personally, I always choose the primitive above the shell. But this is only a preference, and not on the basis of any technical advantages.

+18
Jan 08 '09 at 9:54
source share

In my opinion, the choice between declaring something as int or Integer just comes down to whether null is a valid value for it or not. Autoboxing (and autounboxing) will take care of any conversion problems where the number just needs to be of the same type. Performance (as indicated) is also unlikely to be noticeable in almost all cases.

In addition, int should be a natural choice and will probably be the most productive if it is a problem anyway. If you need to be able to store NULL values, then you have to use Integer (and also ensure that no null references are automatically unpacked for a method that uses just ints, as this will throw a NullPointerException).

+10
Jan 08 '09 at 10:54
source share

Integer theoretically slower than int , however the performance impact should be minimal if you don't crunch the numbers. Also, optimizing JIT will reduce performance loss.

Use the one that is best suited to your situation in terms of a primitive or reference type.

+5
Jan 08 '09 at 9:43
source share

int is 10 times faster than an integer

we test this code with the jetm performance library

 int n; EtmPoint point1 = etmMonitor.createPoint("test:objects"); for (n = 0; n < 1000000; n++) { Integer t = 0; t = 10; t = 11; } point1.collect(); EtmPoint point = etmMonitor.createPoint("test:primitives"); for (n = 0; n < 1000000; n++) { int t = 0; t = 10; t = 11; } point.collect(); etmMonitor.render(new SimpleTextRenderer()); 

and results:
test: objects 10.184
test: primitives 1.151

+4
Nov 04 '10 at 6:30
source share

To give you an idea, 2000 Integer will add about 0.5 ms to your request. If you need to serialize this data, it can add some more.

However, correctness should be the first. It makes no sense to be very fast, but wrong. You must consider null values โ€‹โ€‹and how to handle them. (If the column is NOT NULL), you can use Integer.MIN ___ VALUE or use a long field instead of int and use Long.MIN_VALUE for null. Although it is larger than int, it will still be many times smaller and more efficient than Integer.

+2
Mar 30 '09 at 6:04
source share

I assume that this depends, among other things, on what you use to access the database. With plain old JDBC, you can do with int s, while ORM can silently convert them to Integers anyway. And Integer will let you handle zeros.

+1
Jan 08 '09 at 9:45
source share

int used by Java for most of all calculations. Integer used in all forms of collections except primitive arrays.

The use of a large number of temporary integers with a thrash garbage collector and the use of an unproductive processor in the background, which will cause a general slowdown in everything. Too many temporary downloads per second will cause the CG to go into the โ€œI need memory nowโ€ emergency mode, which can lead to delays in mission-critical applications (for example, real-time interactive graphics, physical device controllers, or communications).

So for me, if I have a lot of nested calls that do not do math but have access to many collections, such as using keys for cards, I use Integer to avoid tons of automatic boxing when passing arguments.

If operations are intensive in mathematics or are used as cycle counters or other mathematical operations and are not stored in collections (except for primitive arrays), I use a primitive. The same applies to all other primitives except String, which is a full-fledged object.

+1
Oct 17 '15 at 17:43
source share

int cannot be used for String using toString or (String) .

Integer can be used for String with toString or (String) and can handle null .

0
Nov 28 '12 at 11:56
source share

If you want to check for null , then Integer better, but if you want to compare an integer, then int might be better. In the following example, I use integer c = 1000 and d = 1000 and compare its return false, but in case of int they will return true.

 public class IntegerCompare { public static void main(String[] args) { int a = 1000; int b = 1000; Integer c = 1000; Integer d = 1000; if (a == b) { System.out.println("int Value Equals"); } if (c == d) { System.out.println("Integer value Equals"); } else { System.out.println("Integer Value Not Equals"); } } } 
0
Jul 12 '17 at 10:03 on
source share

One of the scenarios that needs to be covered is validation.

Imagine we have the following class:

 class Foo{ @Min(value = 10) int val; } 

If the user does not provide a value for val in the request, we will get an unpleasant NumberFormatException .

If we replace int with Integer , we can use @NotNull and solve this problem more elegantly.

0
Mar 05 '19 at 15:39
source share



All Articles