• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Primitive Type/Wrapper Type

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 80613
467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22848
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Campbell and Rob for your valuable replies.
 
Campbell Ritchie
Marshal
Posts: 80613
467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic