• Post Reply Bookmark Topic Watch Topic
  • New Topic

primitives vs. primitive wrappers

 
Mostafa El Hosseiny
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm designing a domain model for wicket/spring/hibernate application (a research repository). I have the following domain entity:

Publication
-id: Long
-title: String
-year: short
-month: byte

I've been reading in the hibernate forum and it was mentioned that using primitive types is *less* efficient than using their wrappers (eg Short, Byte) in hibernate as accessing them using reflection is slower. It's also mentioned in several online blogs and articles (one example is http://blog.vinodsingh.com/2008/04/primitives-vs-objects.html) that primitive wrappers consume more memory. Does that mean that using primitive types is slower in hibernate but faster in spring/wicket (i'm not sure about spring as it uses reflection too.)? And the opposite for primitive wrappers?

Note: I know that this probably isn't an issue for enterprise applications, but i would like to know for educational purposes.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the point of view of a database application this sort of performance distinction is ignorable. Any difference between the two pales into the back ground when compared with the bottleneck of the network conversation between client code and the database.

There are much more pertinant considerations picking between wrappers and primitives: are the fields nullable? Is this code required to be JAX-RPC compliant (e.g. is it intended to ever be exposed as a web service? or called by non-Java clients?)?

The ins and outs of reflection performance will be JVM dependent. Its not surprising wrappers consume more memory; they are wrappers round somthing after all. I suppose you have to think: does it actually matter? To use the example in that blog post: the memory consumption of 1 million objects in a Hibernate session will be way higher than 1 million Integers, and the time taken and resources used to pull that amount of data back from the database is likely to be exponentially higher yet.
 
Mostafa El Hosseiny
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi paul, thanks for replying.

I understand now that for a database application, this is ignorable. I also know that for nullable fields wrappers are recommended. We were considering this for our non-nullable fields. Even though it is ignorable, i wanted to know if primitives for non-nullable fields consumed less memory/are more efficient in the case of a wicket/spring/hibernate application for purely educational reasons.

the code is intended to exposed as a web service, and i'd sure like to know the implications of primitives and wrappers on that.

thanks again.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like I should have checked the docs before answering - my memory is rusty. Primitives are fine in web services.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!