0% found this document useful (0 votes)
5 views2 pages

String Buffer Class

The Java StringBuffer class is a mutable string class that allows modification of string objects and is thread-safe. Key constructors include creating an empty buffer, a buffer with a specified string, and a buffer with a specified capacity. The Scanner class is used for obtaining user input of various primitive types and strings, with methods like next() for reading until a space and nextLine() for reading until a newline.

Uploaded by

noob333pro
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

String Buffer Class

The Java StringBuffer class is a mutable string class that allows modification of string objects and is thread-safe. Key constructors include creating an empty buffer, a buffer with a specified string, and a buffer with a specified capacity. The Scanner class is used for obtaining user input of various primitive types and strings, with methods like next() for reading until a space and nextLine() for reading until a newline.

Uploaded by

noob333pro
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java StringBuffer Class

Java StringBuffer class is used to create mutable (modifiable) String objects. The StringBuffer class in Java is
the same as String class except it is mutable i.e. it can be changed.

Java StringBuffer class is thread-safe i.e. multiple threads cannot access it simultaneously. So it is safe and will
result in an order.

Important Constructors of StringBuffer Class

Constructor Description

StringBuffer() It creates an empty String buffer with the initial capacity of 16.

StringBuffer(String str) It creates a String buffer with the specified string.

StringBuffer(int It creates an empty String buffer with the specified capacity as


capacity) length.

Mutable String:

A String that can be modified or changed is known as mutable String. StringBuffer and StringBuilder classes
are used for creating mutable strings.

Important methods of StringBuffer class

Modifier
Method Description
and Type

public It is used to append the specified string with this string. The
synchronized append(String s) append() method is overloaded like append(char), append(boolean),
StringBuffer append(int), append(float), append(double) etc.

It is used to insert the specified string with this string at the specified
public
insert(int offset, String position. The insert() method is overloaded like insert(int, char),
synchronized
s) insert(int, boolean), insert(int, int), insert(int, float), insert(int,
StringBuffer
double) etc.

public replace(int startIndex,


It is used to replace the string from specified startIndex and
synchronized int endIndex, String
endIndex.
StringBuffer str)

public
delete(int startIndex,
synchronized It is used to delete the string from specified startIndex and endIndex.
int endIndex)
StringBuffer
public
synchronized reverse() is used to reverse the string.
StringBuffer

public int capacity() It is used to return the current capacity.

ensureCapacity(int
public void It is used to ensure the capacity at least equal to the given minimum.
minimumCapacity)

public char charAt(int index) It is used to return the character at the specified position.

It is used to return the length of the string i.e. total number of


public int length()
characters.

substring(int
public String It is used to return the substring from the specified beginIndex.
beginIndex)

substring(int
It is used to return the substring from the specified beginIndex and
public String beginIndex, int
endIndex.
endIndex)

SCANNER CLASS:

The Scanner class in [Link] package used for obtaining the input of the primitive types like int, double, etc.
and strings.

The scanner class consists next() and nextLine() methods.

next() Method: The next() method in java is present in the Scanner class and is used to get the input from the
user. In order to use this method, a Scanner object needs to be created.

This method can read the input only until a space(” “) is encountered. In other words, it finds and returns
the next complete token from the scanner.

nextLine() Method: The nextLine() method in java is present in the Scanner class and is used to get the input
from the user. In order to use this method, a Scanner object needs to be created.

This method can read the input till the end of line. In other words, it can take input until the line change or
new line and ends input of getting ‘\n’ or press enter.

You might also like