Get your CodeRanch badge!*
The moose likes Java in General and the fly likes Primitive Type/Wrapper Type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Primitive Type/Wrapper Type" Watch "Primitive Type/Wrapper Type" New topic
Author

Primitive Type/Wrapper Type

Gagan Grover
Greenhorn

Joined: Mar 02, 2007
Posts: 17
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.


Gags
Walter Gabrielsen Iii
Ranch Hand

Joined: Apr 09, 2011
Posts: 158
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

Joined: Apr 09, 2011
Posts: 158
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

Joined: Mar 02, 2007
Posts: 17
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

Joined: Oct 13, 2005
Posts: 36486
    
  16
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

Joined: Oct 27, 2005
Posts: 19543
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Gagan Grover
Greenhorn

Joined: Mar 02, 2007
Posts: 17
Thanks Campbell and Rob for your valuable replies.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Primitive Type/Wrapper Type
 
Similar Threads
Wrapper Classes
Garbage Collection
Primitive vs Wrapper type in Maping file
Autoboxing
difference between true type Long and primitive type long?