
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 7442 Articles for Java

2K+ Views
In order to change a single character in a StringBuffer object in Java, we use the setCharAt() method. The setCharAt() method sets the character at the index specified as a parameter to another character whose value is passed parameter of the setCharAt() method. The method sets a new character sequence with the only change being the character passed as the parameter at the specified index.Declaration - The java.lang.StringBuffer.setCharAt() method is declared as follows−public void setCharAt(int index, char ch)If index is greater than the length of the StringBuffer object or is negative, an IndexOutOfBoundsException is generated.Let us see a program to ... Read More

185 Views
To display the position of a Substring in Java, we use the lastindexOf() method in Java. The lastindexOf() method returns the rightmost occurrence of the substring within a particular StringBuffer object.If the substring occurs more than once in the StringBuffer object, the index of the first character of the rightmost occured substring is returned. If the substring is not found, the lastIndexOf() method returns -1.Declaration − The java.lang.StringBuffer.lastIndexOf() method is declared as follows−public int lastIndexOf(String s)where s is the substring whose index needs to be foundLet us see a program which displays the position of a Substring.Example Live Demopublic class Example ... Read More

474 Views
In order to change the default capacity of the StringBuffer Object, we use the ensureCapacity() method. When the current capacity is less than the parameter passed, then a new internal array is allocated with greater capacity.The new capacity is the larger of the minimumCapacity argument and twice the old capacity, plus 2. If the minimum capacity is non positive, then it remains dormant and does not take any action.Declaration-The java.lang.StringBuffer.ensureCapacity() method is declared as follows−public void ensureCapacity(int minimumCapacity)Let us see the working of the ensureCapacity() methodExample Live Demopublic class Example { public static void main(String[] args) { ... Read More

186 Views
The capacity() method is a method of the StringBuffer class . It returns the quantity of storage present for recently inserted characters, after which an allocation occurs.Declaration-The java.lang.StringBuffer.capacity() method is declared as follows−public int capacity()Let us see an example program showing how to get the capacity of the StringBuffer Object.Example Live Demoimport java.lang.*; public class Example { public static void main(String[] args) { StringBuffer b = new StringBuffer(“Hello”); // returns the current capacity of the String buffer which is 16 + 5 = 21 System.out.println("Capacity for the StringBuffer Object = " + b.capacity()); } }OutputCapacity for the StringBuffer Object = 21

701 Views
The setLength(int newLength) method is used to change the length of the StringBuffer Object. It sets the length of the character sequence. The character sequence is updated to a new one whose length is determined by the value passed as the parameter in the method. The newLength should be greater than or equal to zero.The java.lang.StringBuffer(int newLength) method is declared as follows −public void setLength(int newLength)Let us see a example program illustrating the use of setLength(int newLength) methodExample Live Demopublic class Example { public static void main(String[] args) { StringBuffer sb = new StringBuffer("Hello World"); ... Read More

895 Views
To find the length of the StringBuffer object, the length() function is used. It is a method of the StringBuffer Class. The method returns the count of characters in the sequence.Declaration- The java.lang.StringBuffer.length() method is declared as follows −public int length()Let us see an example program illustrating the use of length() method −Example Live Demopublic class Example { public static void main(String[] args) { StringBuffer sb = new StringBuffer("Hello World"); int len = sb.length(); // computes the length of the StringBuffer Object System.out.println(len); } }Output11

271 Views
To create a StringBuffer Object, we use the following syntax −StringBuffer s=new StringBuffer();When we create a reference to an object, we use the assignment operator. For example, String s1 = ”hi”; String s2 = s1; // s2 is a reference of s1A program to illustrate the creation of a StringBuffer object using a reference stored in a variable of type String is given below −Example Live Demopublic class Example { public static void main(String args[]) { String input = "hey"; String s1 = input; // creating a reference of the String ... Read More

242 Views
The StringBuffer append() method appends the String representation of the particular argument to the sequence. It is a method of the java.lang.StringBuffer class. This method returns a reference to the object.The method changes the value of the object called in the method. Each primitive data type will have a separate method −public StringBuffer append(double d) public StringBuffer append(float f) public StringBuffer append(int i) public StringBuffer append(String str) public StringBuffer append(long l) public StringBuffer append(char[] str) public StringBuffer append(char[] str, int offset, int len) public StringBuffer append(Object obj) public StringBuffer append(boolean b) public StringBuffer append(char c) public StringBuffer append(StringBuffer sb)ExampleA program ... Read More

180 Views
The StringBuffer append() method appends the String representation of the particular argument to the sequence. It is a method of the java.lang.StringBuffer class. This method returns a reference to the object.The basic syntax for append() method is as follows −public StringBuffer append(data_type variable_name)A program to illustrate the use of append() method is given as follows −Example Live Demoimport java.lang.*; public class Example { public static void main(String[] args) { StringBuffer s1 = new StringBuffer("The sun rises in the east "); System.out.println("Statement : " + s1); s1.append(true); System.out.println("Outcome : ... Read More

339 Views
The capacity() method returns the current capacity. It is a part of the StringBuffer. It is the quantity of storage present for recently inserted characters, after which an allocation occurs.Declaration - The capacity() method is declared in the java.lang.StringBuffer class as follows −public int capacity()The capacity of a StringBuffer object can be set by inserting the value to be set while instantiating the StringBuffer class.Let us see an example program showing how the capacity value can be set.Example Live Demoimport java.lang.*; public class Example { public static void main(String[] args) { StringBuffer b = new StringBuffer(100); ... Read More