Introduction to Java StringBuffer class.
Section 12.1: String Buffer class
Key Points:
- used to created mutable (modifiable) string.
- Mutable: Which can be changed.
- is thread-safe i.e. multiple threads cannot access it simultaneously.
Methods:
- public synchronized StringBuffer append(String s)
- public synchronized StringBuffer insert(int offset, String s)
- public synchronized StringBuffer replace(int startIndex, int endIndex, String str)
- public synchronized StringBuffer delete(int startIndex, int endIndex)
- public synchronized StringBuffer reverse() public int capacity()
- public void ensureCapacity(int minimumCapacity) public char charAt(int index)
- public int length()
- public String substring(int beginIndex)
- public String substring(int beginIndex, int endIndex)
Example Showing difference between String and String Buffer implementation:
public static void main(String args[])
{
String str = “study”;
str.concat(“tonight”);
System.out.println(str); // Output: study
StringBuffer strB = new StringBuffer(“study”);
strB.append(“tonight”);
System.out.println(strB); // Output: studytonight
}
} class Test {
About us and this blog
We are a digital marketing company with a focus on helping our customers achieve great results across several key areas.
Request a free quote
We offer professional SEO services that help websites increase their organic search score drastically in order to compete for the highest rankings even when it comes to highly competitive keywords.
Subscribe to our newsletter!
More from our blog
See all posts
Section110.1:MultithreadedTimersSystem.Threading.Timer-Simplestmultithreadedtimer.Containstwomethodsandoneconstructor. Example: A timer calls the DataWrite method, which writes "multithread executed…"…
Section109.1:UsingStreams Astreamisanobjectthatprovidesalow-levelmeanstotransferdata.Theythemselvesdonotactasdata containers. Thedatathatwedealwithisinformofbytearray(byte[]).Thefunctionsforreadingandwritingareallbyte orientated, e.g. WriteByte(). Therearenofunctionsfordealingwithintegers,stringsetc.Thismakesthestreamverygeneral-purpose,butless simpletoworkwithif,say,youjustwanttotransfertext.Streamscanbeparticularlyveryhelpfulwhenyouare dealing with large…
Section108.1:CheckedandUnchecked C#statementsexecutesineithercheckedoruncheckedcontext.Inacheckedcontext,arithmeticoverflowraisesan exception. In an unchecked context, arithmetic overflow is ignored and…
Section107.1:Asimpleindexer classFoo { privatestring[]cities=new[]{"Paris","London","Berlin"}; publicstringthis[intindex] { get{ returncities[index]; } set{ cities[index]=value; }…
Section106.1:System.Stringclass InC#(and.NET)astringisrepresentedbyclassSystem.String.Thestringkeywordisanaliasforthisclass. The System.String class is immutable, i.e once created its state…
Section105.1:TypesofPolymorphism Polymorphism means that a operation can also be applied to values…
Section104.1:CustomActionFilters We write custom action filters for various reasons. We may have…
Byusingasynchronoussocketsaservercanlisteningforincomingconnectionsanddosomeotherlogicinthe mean time in contrast to synchronous socket when they are listening…
No Comments
Recent Posts
- Chapter110:Timers 10/05/2024
- Chapter109:Stream 09/05/2024
- Chapter108:CheckedandUnchecked 08/05/2024