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