Re: Wrapper classes are Immutable but you use them to make a function parameter a reference?
- From: "Chris Uppal" <chris.uppal@xxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 28 Nov 2006 15:40:31 -0000
marcwentink@xxxxxxxxxxx wrote:
Wrapper classes are Immutable but you use them to make a primitive
function parameter a reference?
Not in the sense that a C++ programmer would mean. The C++ world gives the
word "reference" a very special meaning which is not shared by Java (or,
indeed, by much of the rest of the programming world).
The wrapper classes wrap a primitive value in an immutable object. So instead
of a variable which holds a primitive value, a 32-bit int say, you have a
variable which holds a reference to an object. The word "reference" here means
essentially the same as "pointer" (the variable holds a pointer to an object),
and has /absolutely nothing/ to do with C++'s weird "reference variables"
(where the "reference" is to another variable).
I am studying some java. My main language I am used to program in is
C++. Doing some test questions I am confused. It seems Wrapper classes
for primitives are Immutable classes, so their value cannot be changed.
Correct.
But I understood that you need Wrapper classes to change the value of
say an integer when you use an integer as a function parameter and you
want that integer to change accordingly to the changes in that
function.
Is this not a paradox?
Nope, just a misunderstanding ;-)
That isn't the purpose of the wrapper classes. If you want "value holder"
objects (as the concept is often called) then you can program them yourself.
But before you do so, give some thought to the design which makes you want
them.Usually such designs are flawed. There /are/ exceptions to that rule,
typically when the ValueHolders are used as part of some fairly sophisticated
architecture (such as using the Observer pattern to track changes to their
values). Using them to simulate C++ parameter passing semantics is not one of
the valid uses.
-- chris
.
- References:
- Wrapper classes are Immutable but you use them to make a function parameter a reference?
- From: marcwentink@xxxxxxxxxxx
- Wrapper classes are Immutable but you use them to make a function parameter a reference?
- Prev by Date: Re: confusion of array clone() method
- Next by Date: Re: Strange performance problem
- Previous by thread: Wrapper classes are Immutable but you use them to make a function parameter a reference?
- Next by thread: Create CSV file from resultset in Java
- Index(es):
Relevant Pages
|