Re: Why doesn't Integer have a setValue(int i), and is there a way of changing its value.

From: natG (natgross.rentalsystems_at_verizon.net)
Date: 12/21/03


Date: Sun, 21 Dec 2003 18:51:32 GMT


"bagbourne" <noway@noway.com> wrote in message
news:bs1rtc$4jp$1@hercules.btinternet.com...
> natG wrote:
>
> > "Bent C Dalager" <bcd@pvv.ntnu.no> wrote in message
> > news:brsjhp$t6r$1@tyfon.itea.ntnu.no...
> >
> >>In article <vu3hd8iad8546c@corp.supernews.com>,
> >>Alex Hunsley <lard@tardis.ed.ac.molar.uk> wrote:
> >>
> >>>natG wrote:
> >>>
> >>>
> >>>>Can you please explain "safer"? Do I practice SAFE Java<g>?
> >>>>Seriously, safer in what respect?
> >>
> >>Two cases spring immediately to mind.
> >>
> >>If you use instances of the class as keys in a Map, or you have them
> >>in a Collection that you expect to always be sorted, then immutability
> >>guarantees that your sorting won't inadvertantly get screwed up by
> >>someone calling a setter at inopportune times. With immutable keys,
> >>the only thing that can mess with your sorting is when the
> >>Collection's setters are called. The alternative to immutability would
> >>be for the class to fire events whenever you changed something and the
> >>Collections classes to be sensitive to such events and be able to
> >>auto-resort themselves, but this gets real messy real quick.
> >
> >
> > Question: Suppose:
> > Integer a = new Integer(10);
> > Integer b = new Integer(20);
> > Integer c = new Integer(30);
> > TreeMap tm = new TreeMap();
> > tm.put(a);
> > tm.put(b);
> > tm.put(c);
> > And then:
> > b = new Integer(60);
> > Now, I assume that the second Integer added to the Map, will return an int
value
> > of 60.
> > If so, what happens to the sort at that point?
>
> You have a fundamental misunderstanding. I'm surprised nobody picked up
> on this. You sent a reference to an Integer object (stored in varable
> named "b") to TreeMap.put(). It stored that reference.
>
> Then you create a *totally new instance* of an Integer with 60 in it,
> and put a reference to that object into variable b.
>
> That has nothing to do with the reference that you previously put into
> the TreeMap.
>
> If it helps, think of object variables as smart pointers that cannot be
> corrupted to point to anything else other than what they were declared as.
>
> When you assign using "=", you just change the variable to *reference*
> that new object.

I am surprised at myself as well. It seems like that in hurry to pick an example
of changing keys in HashMap, I blinded myself.
-nat



Relevant Pages

  • Re: Releasing Object Variables
    ... where object variables are not released when they go out ... is not going to create a problematic implicit reference, ... might run the risk of leaving an implicit variable. ... The longer I'm involved in computers professionally, ...
    (comp.databases.ms-access)
  • Re: Nothing Keyword Destories Objects rather than just resetting the variable to an empty variable
    ... undesired effect of the ByRef). ... cause of the local object variable of C was reference to the same ... Yes, an object variable is a pointer to an object, but that is the only ... Though I haven't fully tested it to be sure with object variables, ...
    (microsoft.public.excel.programming)
  • Re: Different between ???.close and Set ??? = Nothing
    ... "Jamie Collins" wrote: ... >> The point of Nothing is to remove the reference to your object so it the RAM ... > releasing 4 bytes and I've yet to see code that sets explicitly ... > you always set object variables to Nothing you risk obscuring the code ...
    (microsoft.public.access.modulesdaovba)
  • Re: Is setting to Nothing really necessary if procedure is about to end?
    ... The source of the problem was an implicitly created by reference ... control call with parens to force evaluation, ... Lyle has frequently pointed out that the ADO libraries don't have ... your ADO object variables. ...
    (comp.databases.ms-access)
  • Re: Anyone point to a tutorial on Collections and Classes
    ... It took me awhile to realize that ... > set it up as a dll. ... Only declare the object variables for your ... you can pass the reference from ...
    (microsoft.public.vb.general.discussion)