Article View: borland.public.jbuilder.java.language
Article #7497Re: How do I change the value of a Boolean
From: Paul Furbacher [Team B]
Date: Fri, 15 Jun 2001 18:54
Date: Fri, 15 Jun 2001 18:54
64 lines
3167 bytes
3167 bytes
Steve Holt wrote:
> this doesn't change the value of ok because its passed by value
>
> change(boolean ok)
> {
> ok = true; // only has an effect in this methods scope
> }
>
> So I figured using the Boolean class may achive this because its passed by
> reference
>
> change(Boolean ok)
> {
> // but no method to change value inside the class
> }
>
> Please don't ask ne to use the return value as this returns something else.
> I'm probably going to get it to throw an exception instead but I'm
> interested to know how you would do this and why the Boolean
> class doesn't provide a method to change the value - weird
> if you ask me!
Yes, at first this might seem weird. However, all of
the classes which essentially "objectify" the primitives
(Boolean for boolean, Integer for int, etc.) are what
can be called "immutable" objects. That means, once
you instantiate the object, its value is not changeable.
This is sort of a fundamental design pattern. The same
is true for String -- once you instantiate a String object
with a given array of characters, you cannot change its
contents. That's why, with String at least, if you
say
String s = "blah";
s = s.replace("h", "b");
you actually get an entirely new object assigned to "s".
The original object reference of "s" is cut loose for
garbage collection.
Other frameworks, such Apple's Cocoa (java.lang.* might
be considered a framework, as much or more than it might
be thought of as a part of the language), have always
(since 1989 when it was NeXT) made the distinction between
mutable and immutable objects. Unfortunately, Java
didn't do this very well from the start and it leads
to some confusion, especially for neophytes. Even arrays,
vectors, hashtables, etc., could be so distinguished, but
Java's inventors sort of overlooked that, too.
May I ask why you are trying to change the value in
this manner instead of just writing
boolean b = [evaluate some condition];
?
Paul Furbacher (TeamB)
Please send responses to the newsgroup only.
That means, do not send email directly to me.
Thank you.
Technical Details
Message-ID:
<3B2A59C9.B80D33AC@mac.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!supernews.com!newsfeed.direct.ca!look.ca!newsfeed.icl.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!dnewsReferences:
<3b2a0bc8_2@dnews>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f74b3,d64ab128ef10f0b9
X-Google-Attributes: gidf74b3,public
X-Google-ArrivalTime: 2001-06-15 12:12:19 PST
Message-ID: <3B2A59C9.B80D33AC@mac.com>
Date: Fri, 15 Jun 2001 14:54:00 -0400
From: "Paul Furbacher [Team B]" <pfurbacher@mac.com>
Reply-To: pfurbacher@mac.com
Organization: Reverse Alchemy, Inc.
X-Mailer: Mozilla 4.75C-CCK-MCD {C-UDP; EBM-APPLE} (Macintosh; U; PPC)
X-Accept-Language: en
MIME-Version: 1.0
Newsgroups: borland.public.jbuilder.java.language
Subject: Re: How do I change the value of a Boolean
References: <3b2a0bc8_2@dnews>
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 24.3.167.167
X-Trace: dnews 992631227 24.3.167.167 (15 Jun 2001 11:53:47 -0700)
Lines: 64
Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!supernews.com!newsfeed.direct.ca!look.ca!newsfeed.icl.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!dnews