Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

StringBuffer capacity

 
Ram Murthy
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Scott Johnson
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Vlado Zajac
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 10374
132
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ram Murthy
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bingo... Now it crystal clear.... Thanks -- JP
 
Mandy Hou
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mandy Hou
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic