GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — Java StringBuffer capacity() Method
Selected Reading
Java StringBuffer capacity() MethodThe Java StringBuffer capacity() method returns the current capacity. The capacity is defined as the amount of storage available for newly inserted characters, beyond which an allocation will occur. In simple words, the default storage of an empty StringBuffer has a 16-character capacity. So, when some characters are already inserted, the method returns the number-of-existing-characters+16 as the output. Therefore, it goes beyond the allocation limit. The capacity differs from the length of a string as the length only calculates the number of characters present in the string, while capacity calculates the maximum number of characters the StringBuffer can fit at the moment. SyntaxFollowing is the syntax for Java StringBuffer capacity() method public int capacity() ParametersThis method does not accept any parameters. Return ValueThis method returns the current capacity. Example: Getting Capacity of StringBufferIf we invoke the method on any StringBuffer object, the return value will be the current capacity. The following example shows the usage of Java StringBuffer capacity() method. package com.tutorialspoint;
public class StringBufferDemo {
public static void main(String[] args) {
StringBuffer buff = new StringBuffer("TutorialsPoint");
// returns the current capacity of the String buffer i.e. 16 + 14
System.out.println("capacity = " + buff.capacity());
buff = new StringBuffer(" ");
// returns the current capacity of the String buffer i.e. 16 + 1
System.out.println("capacity = " + buff.capacity());
}
}
OutputLet us compile and run the above program, this will produce the following result − capacity = 30 capacity = 17 Example: Facing Exception while using nullIf we pass a null value to the StringBuffer object as its input, the method throws a NullPointerException. In the following program, we declare a StringBuffer object and assign a null value to it. public class StringBufferCapacity {
public static void main(String args[]) {
StringBuffer cap = new StringBuffer(null);
System.out.println("The capacity: " + cap.capacity());
}
}
ExceptionIf we try to compile and run the program above, instead of an output, the method throws a NullPointer Exception − Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.length()" because "str" is null
at java.base/java.lang.AbstractStringBuilder.<init>(AbstractStringBuilder.java:117)
at java.base/java.lang.StringBuffer.<init>(StringBuffer.java:158)
at StringBufferCapacity.main(StringBufferCapacity.java:6)
Example: Checking Default Capacity of StringBufferWhen we do not pass any input to the StringBuffer object, it is regarded as an empty StringBuffer. In this case, the method returns the default capacity value. In the following example, we declare a StringBuffer object and no input is assigned to it. Therefore, the StringBuffer is empty. Then, we invoke the method on this StringBuffer trying to obtain the capacity. public class StringBufferCapacity {
public static void main(String args[]) {
// declare an empty StringBuffer
StringBuffer cap = new StringBuffer();
//print the default capacity
System.out.println("The capacity: " + cap.capacity());
}
}
OutputLet us compile and run the program above, the output is displayed as follows − The capacity: 16 java_lang_stringbuffer.htm Advertisements
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |