Reputation: 15574
I have a table in an SQL database where the values' data type is BIGINT(20)
, and I need to get those data to my logic in Java for processing.
What should be the appropriate data type in Java to store the BIGINT(20)
value returned ?
I thought of using Long
, but there is BigInteger
also, can't figure out what is good or suitable.
Upvotes: 16
Views: 21644
Reputation: 653
MySQL BIGINT: min value (-2^63), max value (2^63-1). Maximum unsigned value (2^64-1). https://dev.mysql.com/doc/refman/8.0/en/integer-types.html
Java Long: min value (-2^63), max value (2^63-1). Maximum unsigned value (2^64-1). https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
So, these two types are the same size :)
Upvotes: 14
Reputation: 3704
It depends on the database server. A Java long
is always implemented as 8 bytes since it is part of the standard.
On MSSQL, the bigint
type is 8 bytes, so it is a perfect match.
Upvotes: 5
Reputation: 3858
The BIGINT type is not part of the SQL standard, so there is no guarantee that it will work the same in all databases. However, at least MSSQL, MySQL, Oracle, Postgresql, and IBM DB2 agree that it uses 8 bytes (where most are signed, but in DB2 it's an unsigned 63-bit value ).
Since a Java Long is also 64 bits, it should work fine. Since longs are a lot faster than BigIntegers, so you should almost certainly use a long.
Upvotes: 1