Article View: borland.public.jbuilder.java.language
Article #19548Re: String vs. StringBuffers
From: Steve Sommers
Date: Wed, 21 Jan 1998 00:00
Date: Wed, 21 Jan 1998 00:00
27 lines
1578 bytes
1578 bytes
Lewis Chan wrote:
> I just started to learn Java. I learned that Strings are constant and
> their values cannot be changed after they are created. And that
> StringBuffers support mutable strings and should be used if the string
> needs to be manipulated.
>
> I created the following class which compiles and runs ok. Therefore, it
> seems like I can manipulate a String. Pls help me understand this.
> Thanks.
>
> class ChangeString {
> public static void main(String args[]) {
> String aStr = "string";
> aStr = aStr.toUpperCase();
> System.out.println(aStr);
> }
> }
aStr is a pointer to "string". What you've done is reassigned aStr to
"STRING" (another memory location) with the statement: aStr =
aStr.toUpperCase(); The effect of this is that "string" is still hanging
around in memory somewhere and is now eligible for garbage collection. With
a StringBuffer the same memory location would be affected.
Technical Details
Message-ID:
<34C6C458.80AFFDEE@pop3.concentric.net>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<34C57F41.37D4@netvigator.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,aa0dcfc1946fc312 X-Google-Attributes: gidf74b3,public From: Steve Sommers <ssommers@pop3.concentric.net> Subject: Re: String vs. StringBuffers Date: 1998/01/21 Message-ID: <34C6C458.80AFFDEE@pop3.concentric.net> X-Deja-AN: 318220849 Content-Transfer-Encoding: 7bit References: <34C57F41.37D4@netvigator.com> Mime-Version: 1.0 Reply-To: ssommers@concentric.net To: clewis@netvigator.com Content-Type: text/plain; charset=us-ascii Organization: None Newsgroups: borland.public.jbuilder.java.language