Wrapper Serialization with primitive field

J

Jimmy

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
 
D

Daniel Pitts

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

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.
 
R

Roedy Green

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.
 
J

Jimmy

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
 
E

Esmond Pitt

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top