my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes StringBuffer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "StringBuffer" Watch "StringBuffer" New topic
Author

StringBuffer

rajashree ghatak
Ranch Hand

Joined: Mar 10, 2001
Posts: 151
Hi friends,
I am unable to understand the output of the following code:
1: StringBuffer sb2=new StringBuffer(5);
2: System.out.println(sb2.length());
3: System.out.println(sb2.capacity());
4: //sb2.setCharAt(0,'i'); //compile error.
5: sb2.insert(0,"Beulos");
6: System.out.println(sb2);
7: System.out.println(sb2.length());
8: System.out.println(sb2.capacity());
9: System.out.println(sb2.charAt(1));
Output is :
0
5
Beulos
6
12
e
My Questions are : a.)When we set the size of the StringBuffer by
passing the value 5 in the constructor on line 1,how can we give a String whose
length is 6(greater than the size of the buffer)?
b.) what is logic behind the output of statement on line 8,how is the
capacity 12 when it is already set to 5 when StringBuffer object is
created?
c.)in line 5,when any value > 0 is passed as the first argument in
insert() method, StringIndexOutOfBoundsException occurs though on
passing 0,the above output is printed.why is it so?
d.)why does line 4 give compile error when line 5 doesn't?
thanx in advance.
rajashree.
David Freels
Ranch Hand

Joined: Feb 01, 2001
Posts: 102
Originally posted by rajashree ghatak:
Hi friends,
I am unable to understand the output of the following code:
1: StringBuffer sb2=new StringBuffer(5);
2: System.out.println(sb2.length());
3: System.out.println(sb2.capacity());
4: //sb2.setCharAt(0,'i'); //compile error.
5: sb2.insert(0,"Beulos");
6: System.out.println(sb2);
7: System.out.println(sb2.length());
8: System.out.println(sb2.capacity());
9: System.out.println(sb2.charAt(1));
Output is :
0
5
Beulos
6
12
e
My Questions are : a.)When we set the size of the StringBuffer by
passing the value 5 in the constructor on line 1,how can we give a String whose
length is 6(greater than the size of the buffer)?
b.) what is logic behind the output of statement on line 8,how is the
capacity 12 when it is already set to 5 when StringBuffer object is
created?
c.)in line 5,when any value > 0 is passed as the first argument in
insert() method, StringIndexOutOfBoundsException occurs though on
passing 0,the above output is printed.why is it so?
d.)why does line 4 give compile error when line 5 doesn't?
thanx in advance.
rajashree.


a) The StringBuffer class grows when it needs to.
JavaDoc comment:
If the internal buffer overflows, it is automatically made larger.
b) The internal buffer probably doubles much like a Vector or Hashtable.
I am not sure about c or d.
David
Mark Smith
Greenhorn

Joined: Mar 27, 2001
Posts: 3
Ok, for c) and d) I think i might have your solution - not positive so anyone please correct me. I ran your code with line 4 not commented out and it did compile fine. BUT it did give an ArrayIndexOutOfBounds Runtime Error. This is how i changed the code:
StringBuffer sb2=new StringBuffer(5);
System.out.println(sb2.length());
System.out.println(sb2.capacity());
sb2.append( "testing" );
sb2.setCharAt(0,'i'); //compile error.
sb2.insert(0,"Beulos");
System.out.println(sb2);
System.out.println(sb2.length());
System.out.println(sb2.capacity());
System.out.println(sb2.charAt(1));
Once the buffer was populated with values, it compiled and ran fine. So I am guessing that if the buffer is empty the setCharAt() will fail wheras a null value has no index(??).
Now line 5 will work with a value less than or equal to the length of the buffer.
Hope I am right and this helped
ego hu
Ranch Hand

Joined: Mar 20, 2001
Posts: 53
About the point C, on the Javadoc
Every string buffer has a capacity. As long as the length of the character sequence contained in the string buffer does not exceed the capacity, it is not necessary to allocate a new internal buffer array. If the internal buffer overflows, it is automatically made larger.
public void ensureCapacity(int minimumCapacity)
Ensures that the capacity of the buffer is at least equal to the specified minimum. If the current capacity of this string buffer is less than the argument, then a new internal buffer is allocated with greater capacity. The new capacity is the larger of:
The minimumCapacity argument.
Twice the old capacity, plus 2.
If the minimumCapacity argument is nonpositive, this method takes no action and simply returns.
tvs sundaram
Ranch Hand

Joined: Jan 28, 2001
Posts: 153
1. I feel that when u create a stringbuffer object w/o String argument no length() could be involved on that. The length method must be able to operate upon the String created to return a valid number. That's reason for first result i.e., 0.
2. For the second result iam also surprised. I thought it will be 16 as the Stringbuffer shold allocate room for 16 characters. (Assuming our argument 5 has no meaning). No clue...
3. Insert() method inserts one String in to another. U cannot insert anything within a String. That's why the problem.
try this :
sb2.insert(0,"Beulos");
System.out.println(sb2);
System.out.println(sb2.length());
sb2.insert(6, "Hai");
System.out.println(sb2);
4. I think setCharAt() will work on a previously defined String only. When u try at line no 4, there is no String defined. Hence the error.
Except for 2, I feel I am correct. Java Gurus may please correct me if i am wrong.
Thanks in advance.
Usha Vydyanathan
Greenhorn

Joined: Jan 03, 2001
Posts: 17
Hi,

StringBuffer sb2=new StringBuffer(5);
In this code we are creating a StringBuffer object with capacity 5. If we don't specify the capcity the object would have a default capacity of 16 characters.

regards,
Usha
sajida kal
Ranch Hand

Joined: Mar 22, 2001
Posts: 89
Hi
For point c I checked up the javadoc api for insert method.
public StringBuffer insert(int offset,
Object obj)
It says "The offset argument must be greater than or equal to 0, and less than or equal to the length of this string buffer"
The length was zero when the insert statement is attempted,
so the only valid offset will be zero in this case.
Hope this helps.
Sajida

rajashree ghatak
Ranch Hand

Joined: Mar 10, 2001
Posts: 151
hi all,
thank u all for the replies to my query.i want to say SORRY for the wrong info about the statement on line 4.It doesnot give compile error but a runtime exception.
ego hu had written:
public void ensureCapacity(int minimumCapacity)
Ensures that the capacity of the buffer is at least equal to the specified minimum. If the current capacity of this string buffer is less than the argument, then a new internal buffer is allocated with greater capacity. The new capacity is the larger of:
The minimumCapacity argument.
Twice the old capacity, plus 2.
If the minimumCapacity argument is nonpositive, this method takes no action and simply returns.
could anyone tell if the minimumCapacity parameter passed in the ensureCapacity() is always twice the old capicity,plus 2.
rajashree.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: StringBuffer