 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Steve Macleod Guest
|
Posted: Thu Sep 21, 2006 4:06 am Post subject: Array of String/StringBuffers Initalisation |
|
|
Ok, learning Java and am puzzled by this. When I initialise an array of
String objects, I can use:
String[] myStrArr = new String[4];
....and the strings ready to use.
Strings are objects, so when I came to use StringBuffer arrays, I
though I could use:
StringBuffer[] myStrBfrArr = new StringBuffer[4];
However, I get "invalid pointer exception" errors when I attempt to
use. I have discovered that I have to use the following to initialise
each element to point to a StringBuffer object like this:
for (int r=0; r<dateArr.length; r++) formattedDateArr[r] = new
StringBuffer();
My question is why do StringBuffers require initialisation in this way
but String objects do not? |
|
| Back to top |
|
 |
Matt Humphrey Guest
|
Posted: Thu Sep 21, 2006 6:14 am Post subject: Re: Array of String/StringBuffers Initalisation |
|
|
"Steve Macleod" <steve.macleod (AT) blueyonder (DOT) co.uk> wrote in message
news:1158793564.034607.277990 (AT) m73g2000cwd (DOT) googlegroups.com...
| Quote: | Ok, learning Java and am puzzled by this. When I initialise an array of
String objects, I can use:
String[] myStrArr = new String[4];
...and the strings ready to use.
|
No, they're not. The array of 4 elements (of String references) is ready to
use--the entries are all null. No strings have been created yet. What code
did you run on the supposed strings that demonstrates that they are really
there?
| Quote: | Strings are objects, so when I came to use StringBuffer arrays, I
though I could use:
StringBuffer[] myStrBfrArr = new StringBuffer[4];
However, I get "invalid pointer exception" errors when I attempt to
use. I have discovered that I have to use the following to initialise
each element to point to a StringBuffer object like this:
for (int r=0; r<dateArr.length; r++) formattedDateArr[r] = new
StringBuffer();
My question is why do StringBuffers require initialisation in this way
but String objects do not?
|
There were no strings in that string array either. If you had tried to use
any of the strings you would get the same error. The StringBuffer code is
correct, assuming you want new buffers. Arrays are not automatically filled
with new objects--they are containers into which you put objects.
Now, just to be thorough, arrays really don't contain objects at all. The
hold references to objects.
Matt Humphrey matth (AT) ivizNOSPAM (DOT) com http://www.iviz.com/ |
|
| Back to top |
|
 |
Steve Macleod Guest
|
Posted: Thu Sep 21, 2006 5:17 pm Post subject: Re: Array of String/StringBuffers Initalisation |
|
|
Ok, I think I understand this.
Gonna crack this sooner or later! |
|
| Back to top |
|
 |
Steve Macleod Guest
|
Posted: Thu Sep 21, 2006 5:18 pm Post subject: Re: Array of String/StringBuffers Initalisation |
|
|
Ok, I think I understand this.
Gonna crack this sooner or later!
Thanks for the replies. |
|
| Back to top |
|
 |
Oliver Wong Guest
|
Posted: Thu Sep 21, 2006 10:39 pm Post subject: Re: Array of String/StringBuffers Initalisation |
|
|
"Steve Macleod" <steve.macleod (AT) blueyonder (DOT) co.uk> wrote in message
news:1158841075.278515.126010 (AT) e3g2000cwe (DOT) googlegroups.com...
| Quote: | Ok, I think I understand this.
Gonna crack this sooner or later!
|
Additionally, Strings are a bit special in Java in that they're objects,
but there's some syntactic sugar available so that they sort of behave like
primitives. So avoid using String in your experiments if the goal is to come
up with generalized conclusions of how objects work in Java.
- Oliver |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|