Re: Wrapper classes are Immutable but you use them to make a function parameter a reference?



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




.



Relevant Pages

  • Re: about iostream& ..
    ... I suppose you saw this as a function parameter, ... Look up 'reference' in a good C++ book. ... you'll always see stream types ... In the context above, it means 'reference'. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: VC6 Internal Error, My Fault?
    ... A reference used as a function parameter ... |> | cannot be bound at compile time. ... Chris Val ...
    (alt.comp.lang.learn.c-cpp)
  • Re: VC6 Internal Error, My Fault?
    ... A reference used as a function parameter ... Other times pointers are most natural (e.g., ... in low-level code which uses the new operator directly). ...
    (alt.comp.lang.learn.c-cpp)
  • Re: pointer/ref question
    ... >I know I have the habit of saying pass by reference when a pointer is being ... I think the phrase "pass by reference" has been grandfathered into C and ... hear anything like "the function parameter uses pass-by-reference", ...
    (alt.comp.lang.learn.c-cpp)
  • Re: programming concepts > specific languages
    ... Am I passing by reference? ... In Java, ... You might complain that I'm using primitives here, ... > behave another. ...
    (comp.programming)