Difference Between Length and Capacity in Java Last Updated : 18 May, 2023 Comments Improve Suggest changes Like Article Like Report Length and capacity are two different things in Java. Length basically increases when characters are appended to the string whereas the capacity increases when the current capacity is exceeded by a new length. length () method is a part of the Java String class. It is used to find the length of the string. It basically counts the number of characters present in the string. Therefore the method returns an integer. The integer is nothing but the character count. It also counts the space as characters. It is a final variable that is applied only on string objects. Syntax: public int length () Example: Java // Java Program to demonstrate // the length function public class length_string { public static void main(String args[]) { String s = "GeeksforGeeks Java"; System.out.println("String length " + s.length()); // Output is String length 18 } } OutputString length 18 The capacity () method is a part of the StringBuffer class. It basically denotes the amount of space available to store new characters. The method returns the current capacity of the string buffer. By default, an empty StringBuffer contains 16 character capacity. So when new characters are added the output is the length of characters+16 is returned. Syntax: public int capacityDifference Between Length and CapacityS. NoLengthCapacity1.It is a part of the String class. It is part of the StringBuffer class. 2.It is used to find the length of the string. It is used to find the capacity of StringBuffer. 3.By default, the length of an empty string is 0.By default, an empty string buffer contains 16 character capacity. 4.It is the actual size of the string that is currently stored. It is the maximum size that it can currently fit. 5.One can count the length by simply counting the number of characters. The capacity can be calculated by counting the number of characters and adding 16 to it. 6.Length is considered while copying elements from one array to anotherCapacity is not considered while copying elements.7.Length can be modified only by removing elements from the arrayCapacity can be increased by reallocating the memory. Example 1: The file name is capacity_example.java Java // Java Program to demonstrate // the capacity function public class capacity_example { public static void main(String args[]) { StringBuffer s = new StringBuffer(); System.out.println("capacity:" + s.capacity()); // The output is capacity:16 (because default capacity is 16) } } Outputcapacity:16 Example 2: The file name is capacity_example.java Java // Java Program to demonstrate // the capacity function public class capacity_example { public static void main(String args[]) { StringBuffer s = new StringBuffer("Gfgstring"); System.out.println("capacity:" + s.capacity()); // The output is capacity:25 (because 16+9 where // length of Gfgstring is 9) } } Outputcapacity:25 Comment More infoAdvertise with us Next Article Difference Between Length and Capacity in Java jhimlic1 Follow Improve Article Tags : Java Difference Between Java-Strings java-StringBuffer Practice Tags : JavaJava-Strings Similar Reads Difference Between Long and BigInteger in Java In Java, long and BigInteger are two different data types used to represent and perform operations on the large integers. In this article we will learn about BigInteger and Long in Java. long in Javalong is a primitive data type in Java used to store 64-bit signed integers. It has a range of values 3 min read Difference between String and Character array in Java Unlike C/C++ Character arrays and Strings are two different things in Java. Both Character Arrays and Strings are a collection of characters but are different in terms of properties. Differences between Strings and Character Arrays:PropertyStringCharacter ArrayDefinitionA sequence of characters is r 3 min read Difference Between byte, short, int and long Datatype in Java There are two types of data types namely primitive datatype/fundamental and non-primitive/derived datatype. The primitive data type is defined as local sets or default set of datatype already present while deriving a datatype or creating a set of datatype using them are known as derived data types s 4 min read Difference between Length and Height Length measures how long something is while Height measures how tall something is. Length is measured from one end to the other end in a horizontal direction while height is measured in a vertical direction. Let's understand the key difference between length and height. What is Length?Length is defi 4 min read Difference between Array and String in Java An array is a collection of similar type of elements that are stored in a contiguous memory location. Arrays can contain primitives(int, char, etc) as well as object(non-primitives) references of a class depending upon the definition of the array. In the case of primitive data type, the actual value 5 min read Difference between Arrays and Collection in Java An array in Java is a group of like-typed variables referred to by a common name. Arrays in Java work differently than they do in C/C++. Following are some important points about Java arrays. On the other hand, any group of individual objects which are represented as a single unit is known as the co 3 min read Difference between jquery.size() and jquery.length JQuery.size() method gives us the number of elements present. For Example, if we are calling the size() method for "p" tag, then it will return the number of "p" tags present on our page. Syntax: $(selector).size() Return value: It returns the number of âselectorâ present. Example: HTML <!DOCTYPE 2 min read Difference between length of Array and size of ArrayList in Java Array and ArrayList are two different Entities in Java. In this article we will learn the difference between length of Array and size of ArrayList in Java.Array has length property which provides the length of the Array or Array object. It is the total space allocated in memory during the initializa 2 min read Difference between ArrayList and HashSet in Java Here are couple of differences between ArrayList and HashSet. Inheritance: Implementation: Implementation : ArrayList implements List interface while HashSet implements Set interface in Java.Internal implementation: ArrayList is backed by an Array while HashSet is backed by an HashMap.Duplicates : A 3 min read Buffer capacity() method in Java with Examples The capacity() method of java.nio.Buffer Class is used to return this buffer's capacity. Syntax: public final int capacity() Return Value: The capacity of this buffer Below are the examples to illustrate the capacity() method: Examples 1: Java // Java program to demonstrate // capacity() method impo 2 min read Like