please clarify regarding capacity method of my below string buffer class example

please clarify regarding capacity method of my below string buffer class example

hi

here is the program below:

> import java.lang.*; public class
> Compare{ public static void
> main(String[] args) {
> System.out.println("StringBuffer
> insert and append example!");
> StringBuffer sb = new StringBuffer(0);
> //First position
> System.out.println(sb.insert(0,
> "vinoda")); int len = sb.length();
> System.out.println("length
> is"+sb.length());
> System.out.println("capacity
> is"+sb.capacity()); //last position
> System.out.println(sb.insert(len,
> "Deepak"));
> System.out.println("capacity
> is"+sb.capacity()); //Six position
> System.out.println(sb.insert(6,
> "Raj")); System.out.println("length
> is"+sb.length());
> System.out.println("capacity
> is"+sb.capacity()); //Always last
> System.out.println(sb.append("Mohit"));
> System.out.println(sb.append("raju"));
> System.out.println("length
> is"+sb.length());
> System.out.println("capacity
> is"+sb.capacity()); } }

o/p:

StringBuffer insert and append example!
vinoda
length is6
capacity is6
vinodaDeepak
capacity is14
vinodaRajDeepak
length is15
capacity is30
vinodaRajDeepakMohit
vinodaRajDeepakMohitraju
length is24
capacity is30

my question is how the capacity of s.buffer changed from 6 to 14 and to 30 and why it remained in 30 for the last operation.
thanks in advance

View Answers









Related Tutorials/Questions & Answers:
please clarify regarding capacity method of my below string buffer class example
please clarify regarding capacity method of my below string buffer class... class > Compare{ public static void > main(String[] args) { >...: StringBuffer insert and append example! vinoda length is6 capacity is6
Please clarify my doubt
Please clarify my doubt  /here is my sample code for deadlock/ class... A.last()"); } } class B { synchronized void bar(A a) { String... is that class A has 2 synchronized methods.Object a of class A is calling a.last method
Advertisements
(ii) String and String Buffer Class
(ii) String and String Buffer Class  difference between String and String Buffer Class
(ii) String and String Buffer Class
String and String Buffer Class  differences between : String and String Buffer Class
(ii) String and String Buffer Class
String and String Buffer Class  difference between String and String Buffer Class... whereas String buffer objects are not constants but growable
Get the limit position and capacity of a double type buffer.
Get the limit position and capacity of a character buffer. In this tutorial you will see how to get the limit position and capacity of a character buffer. The given example illustrate the use of limit() method which returns the buffer
JDOM CDATA Example, Use of append(String str) method of CDATA class.
JDOM CDATA Example, Use of append(String str) method of CDATA class. In this tutorial, we will implement append() method of  CDATA class... with a string. The setContent(Content content ) method of Element class is used to set
Get the capacity of a buffer
Get the capacity of a buffer In this tutorial we will see how to get size of buffer using capacity() method. Code...;{   public static void main(String[] args) 
Java bigdecimal toString example
;    Example below demonstrates working of bigdecimal class toString method. Method return type is string therefore method returns string value only. This bigdecimal class toString method generates, string representation
Get the limit position and capacity of a character buffer.
Get the limit position and capacity of a character buffer. In this tutorial you will see how to get the limit position and capacity of a character buffer. The given example illustrate the use of limit() method which returns the buffer
Java bigdecimal toPlainString example
;    Example below demonstrates working of bigdecimal class toPlainString method. Method return type is string therefore method returns string value only. This bigdecimal class toPlainString method generates, string
Create a float buffer by using wrap method of FloatBuffer class.
Create a float buffer by using wrap method of FloatBuffer class. In this tutorial, we will see how to create a float buffer by using wrap method...[] array)  The wrap() method create a float buffer by wrapping 
Get the capacity of a double type buffer
Get the capacity of a double buffer In this tutorial we will see how to get size of a double buffer using capacity() method. Code...;DoubleBufferDemo {   public static void main(String[] args
String indexOf method example
.style1 { font-size: medium; } String indexOf() method example:- String class method indexOf() is use to find the starting character position of the substring from the string. The indexOf() method start to search from
Create a short buffer by using wrap method of ShortBuffer class.
Create a short buffer by using wrap method of ShortBuffer class. In this tutorial, we will see how to create a shortt buffer by using wrap method...[] array)  The wrap() method create a short buffer by wrapping 
Use of hasRemaining() method in float buffer class.
Use of hasRemaining() method in float buffer class. In this tutorial, we will see the use of  hasRemaining method of Buffer class in FloatBuffer...; The allocate(..)method allocate a new float buffer. final boolean
clarify date class - Java Beginners
clarify date class  Dear, clarify this question... "Develop Date class in Java similar to the one available in java.util package. Use JavaDoc... in java.util or any packages.. please. thanks in advance. regards S.v.Doss
Example for a method in java which returns a class
Example for a method in java which returns a class  Hi, I want to create a 1 class,and declare some methods there.I need to method must returns a classname.when i call that particular method(which returns a class),how can i
Clarify my doubt - JSP-Servlet
Clarify my doubt  Hi All, Although i know the concept of Servlet & JSP, but I have never got a chance to work in EJB . So could u please tell me how to create web Project in netbeans and deploy it in tomcat AND/OR creating a web
Java bigdecimal negate method example
Java bigdecimal negate method example       Example below demonstrates working of bigdecimal class negate method. Method returns the same copy of the bigdecimal value
Use of isDirect() method of byte buffer class in java.
capacity)  The allocate() method allocate a byte buffer...Use of isDirect() method of ByteBuffer class in java. In this tutorial... isDirect() The isDirect() method tells whether this associated buffer
clarify my question - Java Beginners
clarify my question  Dear Sirs/Expert, Thanks for writing back... the matching tutors to the parent. I was telling you that my degree... and the locations for example, there will be a table that display the following
Java bigdecimal multiply method example
Java bigdecimal multiply method example       Example below demonstrates bigdecimal class multiply method example. Method returns a bigdecimal object value. Method
String substring method example
.style1 { font-size: medium; } String substring() method example... a string. String class provide methods for finding substring from string. Method... string. In this example we have used this method. Example:-ADS_TO_REPLACE_1
Java bigdecimal subtract method example
Java bigdecimal subtract method example       Example below demonstrates working of bigdecimal class subtract method. Method returns bigdecimal object values as per context
Get Class Name
; In the example given below we will learn how to get name of the particular class with package name. getName() method returns fully qualified name of the particular class in string format.  ADS_TO_REPLACE_1     Here
String equalsIgnoreCase(String Str)
explanation about the equalsIgnoreCase(String Str) method of String class. We are going to use equalsIgnoreCase(String Str) method of String... of the method. Description of the code: As shown in the example below we have taken
String intern()
the intern() method of String class. We are going to use intern() method of String class in Java. The description of the code is given below for the usage of the method. Description of the code: As shown in the example we have created
Clarify my last question's answer - Java Beginners
Clarify my last question's answer  Dear expert, I've tried out... expert's advise. Many thanks.   Hi Friend, Please clarify your... did not specify my question too clearly. Here's my 2 database
String indexOf() method in Java
String indexOf() method in Java In this tutorial we will discuss about indexOf() method in java. indexOf() method which is of string class,  used to locate a character or string.  The String class provide two method
How to clean a buffer using clear method in java.
;    In this example, the clear() method clean a buffer for further... will be capacity of buffer. The method allocate ( int capacity) creates a new byte...; The allocate() method create a byte buffer of specified capacity
String indexOf() Method
the indexOf() method of String class. We are going to use indexOf() method of String class in Java. The description of the code is given below for the usage... String indexOf() Method      
Java bigdecimal plus method example
Java bigdecimal plus method example       Example below demonstrates working of bigdecimal class plus method. Method always returns a bigdecimal value of positive nature as per
String Concat()
() method of String class. We are going to use Concat() method of String class in Java. The description of the code is given below for the usage of the method... String Concat()      
String Determining a Character's Unicode Block
explanation about the UnicodeBlock() method of String class. We are going to use UnicodeBlock() method of String class in Java. The description of the code is given below for the usage of the method. Description of the code: As shown
Java method Overriding
Below example illustrates method Overriding in java. Method overriding in java means a subclass method overriding a super class method. Superclass... class. In the example class B is is the sub class and class A is the super class
regarding css file in my eclipse
regarding css file in my eclipse  hai frns i want a small help frm u.when ever i use css file in html or normal web browsers the output is coming... the output is not changing can anyone please tell me a solution regarding
Regarding method returns multiple values
Regarding method returns multiple values  Hi, 1)I have a requirement like,1 method returns multiple values.how to do that one. 2)can i use method, for example like getFile()[] in this way or not. plzzzzzzzzzzzzzzzzzz provide
String getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
the getChars() method of String class. We are going to use getChars() method of String class in Java. The description of the code is given below for the usage of the method. Description of the code: As shown in the example we have taken
Java bigdecimal signum example
;    Example below demonstrates working of bigdecimal class.... This bigdecimal class signum method generates, sign of the bigdecimal object value. After..., 0, +1.  For example if the bigdecimal value is negative,  method
Java bigdecimal multiply example
Java bigdecimal multiply example       Example below demonstrates bigdecimal class multiply method example. Method returns a bigdecimal object value. Method multiply 
Java bigdecimal toBigIntegerExact example
;     Example below demonstrates working of bigdecimal class toBigInteger  method. Method default return type is a biginteger class object value.  If the bigdecimal object value is 423.0, method
String substr method example
.style1 { font-size: medium; } String substr() method example:- String class provides substr() method for find substring from string. This method...:Application> In this example, we have used this method with a string
Java bigdecimal toBigInteger example
Java bigdecimal toBigInteger example       Example below demonstrates working of bigdecimal class toBigInteger  method. Method default return type is a biginteger
Java bigdecimal movePointLeft example
;    Example below demonstrates bigdecimal class movePointLeft method example. Method returns a bigdecimal object value. Method moves... inside the parentheses of the method.  In the example two bigdecimal
Java BigDecimal movePointRight example
;    Example below demonstrates bigdecimal class movePointRight method example. Method returns a bigdecimal object value. Method moves... passed inside the parentheses of the method. In the example one bigdecimal 
Use of hasCode method of FloatBuffer class in java.
Use of hashCode method of FloatBuffer class in java. In this tutorial, we will see the use of hashCode() method of FloatBuffer class in java... Method Description static FloatBuffer allocate( int capacity
String replaceFirst() Method
. We are going to use replaceFirst() method of String class in Java. The description of the code is given below for the usage of the method. Description... String replaceFirst() Method     
Use of toString() method of FloatBuffer class.
; The allocate(..) method allocate a float buffer of given capacity... Capacity : 512 String representation of the float buffer state...Use of toString() method of FloatBuffer class. In this tutorial, we will see
Java bigdecimal pow method example
Java bigdecimal pow method example       Example below demonstrates working of bigdecimal class pow method. Method returns a bigdecimal value (to the power n) i.e. (this.object

Ads