Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2007

Tip: Looking for answers? Try searching our database.

Wrapper Serialization with primitive field

Thread view: 
Jimmy - 30 Jul 2007 18:50 GMT
What's the advantage or disadvantage for having a wrapper to contain
primitive private member (i.e. private int count; // inside public
class Wrapper)?

Or is it better to not use primitive inside a serializable wrapper
(i.e. private Integer count; // inside public class Wrapper)?

Thanks for comment,
Jimmy
Daniel Pitts - 30 Jul 2007 21:42 GMT
> What's the advantage or disadvantage for having a wrapper to contain
> primitive private member (i.e. private int count; // inside public
[quoted text clipped - 5 lines]
> Thanks for comment,
> Jimmy

Generally, its better OO design to wrap primative values in a
meaningful class.  Instead of "int distanceInMeters", you would have a
class Distance { private int meters; }, with the appropriate
getMeters() method.  This allows you to decouple the actual storage
value (integer meters) with the representation (some Distance).

This allows you to change the Distance class to use, for instance,
centimeters or inches, without changing ANY code that references
Distance (the Distance class would do the necessary conversions in the
getMeter/setMeter methods, and you would add getInch/setInch, etc...)

As for serialization, this allows you to version the value as well.
You can intercept the serialization and do any necessary conversions
in one place, rather than having to do it for the same primitive value
in many places...

Using primitive values too often is sometimes known as the Primitive
Obsession code-smell (Refactorying, Martin Fowler).

If your value has a semantic meaning, and you don't have an
*overwhelming* reason not to, you should wrap your primitives in
meaningful classes.
Roedy Green - 31 Jul 2007 14:50 GMT
>What's the advantage or disadvantage for having a wrapper to contain
>primitive private member (i.e. private int count; // inside public
>class Wrapper)?

If you write a naked int to a ObjectOutputStream it will be autoboxed
into an Integer.  See http://mindprod.com/jgloss/autoboxing.html

If you have an int field in a  class vs a link to an Integer Object,
the stream will be more compact, as will RAM. It will be written as a
primitive int.

It can be fun to look at serialised object streams with a hex editor.
You can learn quite a bit even if you only understand a small fraction
of the format.  See http://mindprod.com/jgloss/serialization.html
for a link to the format spec.

Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Jimmy - 31 Jul 2007 16:37 GMT
Thanks for feedback guys.  I'll change the wrapper to contain
primitive.  However, there 2 parts of project as 1st part using JDK
1.4 and 2nd part using JDK 1.5.  Serialized wrapper (value-object) is
being passed back-and-forth.

It should be ok?

Thanks,
Jimmy
Esmond Pitt - 01 Aug 2007 12:04 GMT
If you write a naked int to an ObjectOutputStream via writeInt(), it
will be written as four bytes. If you write an int or an Integer via
writeObject(), there will be an item tag plus the same four bytes. (See
java.io.Bits.) Autoboxing just makes the last two cases equivalent.
There is no 1.4/1.5 issue to be concerned with.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.