• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Primitive Type/Wrapper Type

 
Gagan Grover
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When we are making a class that has an id field which is Bigint in database which type should be used? Should a long primitive type be used or Long wrapper type? Please clarify.
 
Walter Gabrielsen Iii
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I think first you should look up what a "bigint" means for your specific database type (because different languages can have their own definition of numerical value), and next you need to compare it to the Java numeric types.

For example, in SQL Server 2000, from int, bigint, smallint, and tinyint:
bigint
Integer (whole number) data from -2^63 (-9,223,372,036,854,775,808) through 2^63-1 (9,223,372,036,854,775,807). Storage size is 8 bytes.


And Java, from Java's Primitive Data Types:
long

8 bytes signed (two's complement). Ranges from -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807.


So, it seems for SQL Server 2000, the Java type you want is long.
 
Walter Gabrielsen Iii
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh wait I mixed up the question, sorry.

You would use a primitive type if you are putting it inside a class as part of a larger whole (such as you storing other values with it).

You would use a wrapper object Long if you intent to only use the id by itself and want to put it into a collection object (List, set, map,...).
 
Gagan Grover
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Walter for your reply, Is there anyway that Wrapper types are more preferable over primitive types, and is this a design practice to always use wrapper types in POJO.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no need to declare any of those values as Long.Sorted out by autoboxing. Search this forum; there are threads about wrapper classes and boxing active at the moment.

But be careful about BigInts; they can be marked unsigned. I do not know what will happen to values ≥ 9,223,372,036,854,775,808. Maybe they will be converted to negative values since long is a two's complement type. But I am not at all sure; try it. It doesn't say in the documentation for the ResultSet method.
 
Rob Spoor
Sheriff
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:There is no need to declare any of those values as Long.

Except if the field can be NULL in the database. You could try using a value like -1, 0 or Long.MIN_VALUE instead of a null Long, but only if you're 100% sure that these values will never be actual values in the database.
 
Gagan Grover
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell and Rob for your valuable replies.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It said in the ResultSet API that this method returns 0 if the value in the database was NULL.

That might, as you point out, Rob, be a drawback in the getLong method.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic