views:

1145

answers:

7

Hi,

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

If I am defining the field as Integer, will there be any performance impact because of 'Integer' type if I am going to retrieve more than 2000 records from DB!?

Thanks in advance.

+3  A: 

Integer is theoretically slower than int, however the performance impact should be minimal unless you are crunching numbers. Also JIT optimizations will reduce the performance loss.

Use the one that better suits your situation in terms of primitive or reference type.

Mehrdad Afshari
+18  A: 

Integer is a better option, as it can handle null. For int null would become 0, silently. Or even if not, you can't do much about it, as int can't express null.

Performance is of little concern here.

  • If you choose int, you will end-up adding extra handling code a lot. And it will benefit you nothing in the end. Your code will not look clean and straight, lot of boiler-plate code, and you wouldn't even gain performance.
  • Let me make it clear, for databases, null is not same as zero. Sometimes you end-up entering 0, where null was intended.
  • Imagine the case where user submitted a form, and doesn't supply any value for int. You will end up getting 0 by default. It makes sense, when that field is not null in the database, but who knows you might refactor your table, down the road, to make that nullable.
Adeel Ansari
Adeel, I think you should edit your answer to include the information in your comments. They are relevant to this discussion and might be missed by some people just glancing at the answer.
William Brendel
@Adeel: How would null silently become 0?
Hemal Pandya
@ William Brendel. I included my comments into original post. Thanks.
Adeel Ansari
@ Hemal: http://java.sun.com/javase/6/docs/api/java/sql/ResultSet.html#getInt(int)
Adeel Ansari
For more info on the null<->int problem and other ORM issues, take a look at [Impedance Mismatch][1] [1]: http://en.wikipedia.org/wiki/Object-Relational_impedance_mismatch
MrWiggles
getInt(int) returns an int - null will already have been silently converted to 0. Instead you should use isNull() in your DAO to check nullable columns, and then you can either use a null Integer, or a magic number (-1) in your model, depending on your needs.
JeeBee
Performance might be of concern once you start using this code. If your code includes a lot of "int blah = object.getBlah(); doCalcsOn(blah); object.setBlah(blah)", then this code will incur at least one new object creation every time it's executed. Something to keep in mind.
wds
A: 

I guess it depends among other things on what you are using for accessing the database. With plain old JDBC you could do with ints, while an ORM could silently convert them to Integers anyway. And Integer would allow you to handle nulls.

agnul
+4  A: 

You should really make your decision based on what you need your object to do rather than the performance costs. Deciding based on performance should be done once a speed issue has been identified with a profiler - the root of all evil and all that.

Look at some of the features of both and use that for your decision, e.g.

Integer can be null, int cannot (so is the int in the DB a NULLable field?) Do you need access to the Integer class methods? Are you doing arithmetic?

Personally I always opt for the primiive over the wrapper but that's just a preference thing rather than based on any technical merit

MrWiggles
auto-boxing would take care of it, if it happens to be once. Otherwise, cast it before performing any heavy calculation.
Adeel Ansari
True, but if you're doing multiple calculations dotted around you might want to go with the int option to avoid the multiple casts
MrWiggles
+4  A: 

To my mind, the choice between declaring something as int or Integer simply comes down to whether null is a valid value for it or not. Autoboxing (and autounboxing) will take care of any conversion issues where the number simply must be one type or another. Performance (as has been pointed out) is also unlikely to be noticable in almost all cases.

Besides, int should be the natural choice, and is likely to be the most performant should that be an issue anyway. If you need to be able to store nulls, then you have to use Integer (and also ensure that no null references are auto-unboxed for a method that takes simply ints as this will result in a NullPointerException).

Andrzej Doyle
A: 

To give you an idea, 2000 Integer would add about 0.5 ms to you query. If you have to serialize this data it could add quite a bit more.

However, correctness should come first. There is no point being very fast but wrong. You have to consider null values and how you handle them. (Unless the column is NOT NULL) You could use Integer.MIN___VALUE or you could use a long field instead of int and use Long.MIN_VALUE for null. Even though it is larger than int, it would still be many times smaller and more efficient than Integer.

Peter Lawrey