aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes StringBuffer capacity Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "StringBuffer capacity" Watch "StringBuffer capacity" New topic
Author

StringBuffer capacity

Ram Murthy
Ranch Hand

Joined: Aug 02, 2005
Posts: 91
1.
public class Test25{
public static void main(String a[]){
StringBuffer sb = new StringBuffer(8);
sb.append("TestString");
System.out.println(sb.capacity());
System.out.println(sb.length());
}
}

The o/p of this is
18
10

Can anyone explain what capacity() is and why it returns 18.

2.
public class Test23{
public static void main(String a[]){
String s1 = "01234";
System.out.println(s1.substring(5));
}
}

Shouldn't this give StringIndexOutOfBoundsException error since we don't have index 5.
When I tested it actually gave no error and o/p came as spaces.

Please explain..

Thanks,
JP


Cheers,
Ram
Scott Johnson
Ranch Hand

Joined: Aug 24, 2005
Posts: 518
Can anyone explain what capacity() is and why it returns 18.

The JavaDoc
is an excellent reference for this type of question. It says:

public int capacity()

Returns the current capacity. The capacity is the amount of storage available for newly inserted characters, beyond which an allocation will occur.

Shouldn't this give StringIndexOutOfBoundsException error


No, this call should return an empty string.
[ October 13, 2005: Message edited by: Scott Johnson ]
Thomas Drew
Ranch Hand

Joined: Sep 15, 2004
Posts: 47
The StringBuffer(8) create a StringBuffer with a capacity of 8 but no character so a length of 0 . You appended 10 characters to the StringBuffer so it how has a capacity of 18 and a length of 10.
Ram Murthy
Ranch Hand

Joined: Aug 02, 2005
Posts: 91
I am still not clear on capacity() and how we arrived at 18. I changed the line in above code to

StringBuffer sb = new StringBuffer(9);
sb.append("TestString");
System.out.println(sb.capacity());

In this case the output of sb.capacity() is 20... how did it arrive at the number. I read throught the sun documentation it doesn't also say how that number is calculated.
Looks like capacit() is what the JVM decides...in such case will we get such kind of questions in the actual certification

As for my second question can you let me know why we dont get StringIndexOutOfBoundsException, even though the String doesn't have index 5.

Thanks,
JP
srikanth reddy
Ranch Hand

Joined: Jul 28, 2005
Posts: 252
yes ravi ..
i have tried for capacity ....
length is ok..it gives us the current number of elements present in the list...
regarding capacity ...when we declare in the constructor as

new StringBuffer(int capacity)...it takes it ..as long as the actual characters have exceeded...as soon as it exceeds...it is adding random value..

please clarify if iam wrong..

sri


Thanks & Regards<br /> <br />-Srikanth
Vlado Zajac
Ranch Hand

Joined: Aug 03, 2004
Posts: 245
THe cpacity is automatically increased when needed probably using ensureCapacity() method. This method specified that the new capacity is the larger of required capacity and current capacity * 2 + 2. (Or unchanged if current capacity is large enough).

8*2+2=18
9*2+2=20
[ October 14, 2005: Message edited by: Vlado Zajac ]
Ram Murthy
Ranch Hand

Joined: Aug 02, 2005
Posts: 91
Thanks I am now clear about capacity and how it is calculated.

I am still looking for some explanation on my second question

2.
public class Test23{
public static void main(String a[]){
String s1 = "01234";
System.out.println(s1.substring(5));
}
}

Shouldn't this give StringIndexOutOfBoundsException error since we don't have index 5.
When I tested it actually gave no error and o/p came as spaces.

Why is this not giving StringIndexOutOfBoundsException even though there are only 4 index positions.

Thanks,
JP
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 4926
    
  10

ravi,

from the java api:



so when exception is thrown, if index negative or larger than length of String-object. in your example length = 5, so no error but empty string. if you would take substring with index 6 or more, you will get the runtime exception.


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Ram Murthy
Ranch Hand

Joined: Aug 02, 2005
Posts: 91
Bingo... Now it crystal clear.... Thanks -- JP
Mandy Hou
Greenhorn

Joined: Oct 15, 2005
Posts: 21
public class Test25{
public static void main(String a[]){
StringBuffer sb = new StringBuffer(8);
sb.append("TestString");
System.out.println(sb.capacity());
System.out.println(sb.length());
}
}

The o/p of this is
18
10

Can anyone explain what capacity() is and why it returns 18.

A:Because the new String length to append is grater than the stringbuffe capacity.the stringbuffe must expend it capacity .
the new capacity = (old capacity +1) *2. It is 18 when old capacity is 8


Regards<br />Mandy<br /> <br />-----------------<br />Thinking,Thinking,Thinking,<br />Before you do any action!!!
Mandy Hou
Greenhorn

Joined: Oct 15, 2005
Posts: 21
2.
public class Test23{
public static void main(String a[]){
String s1 = "01234";
System.out.println(s1.substring(5));
}
}

Shouldn't this give StringIndexOutOfBoundsException error since we don't have index 5.
When I tested it actually gave no error and o/p came as spaces.

A:this is sourcecode of that substring() method.
public String substring(int beginIndex, int endIndex) {
if (beginIndex < 0) {
throw new StringIndexOutOfBoundsException(beginIndex);
}
if (endIndex > count) {
throw new StringIndexOutOfBoundsException(endIndex);
}
if (beginIndex > endIndex) {
throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
}
return ((beginIndex == 0) && (endIndex == count)) ? this :
new String(offset + beginIndex, endIndex - beginIndex, value);
}

In you seccsion.the beginindex is 5,an endIndex is 5 to.So it never throw exception but print an empty string. if the beginindex is 6,it will throw encepttion as you except.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: StringBuffer capacity
 
Similar Threads
doubt with string predefined method
capacity of a stringbuffer
String & String Buffer
Please explain me the output in detail
Null passed into constructor