Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Java StringBuilder Class
Next article icon

StringBuffer Class in Java

Last Updated : 18 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The StringBuffer class in Java represents a sequence of characters that can be modified, which means we can change the content of the StringBuffer without creating a new object every time. It represents a mutable sequence of characters.

Features of StringBuffer Class

The key features of StringBuffer class are listed below:

  • Unlike String, we can modify the content of the StringBuffer without creating a new object.
  • StringBuffer has an initial capacity, and it can also be adjusted later with the help of the ensureCapacity() method.
  • With the help of the append() method, we can add characters, strings, or objects at the end of the StringBuffer.
  • With the help of the insert() method, we can insert characters, strings, or objects at a specified position in the StringBuffer.
  • With the help of the delete() method, we can remove characters from the StringBuffer.
  • With the help of reverse() method, we can reverse the order of characters in the StringBuffer.

Example: Here is an example of using StringBuffer to concatenate strings:

Java
//Demonstrating String Buffer public class Geeks {     public static void main(String[] args){              	// Creating StringBuffer         StringBuffer s = new StringBuffer();              	// Adding elements in StringBuffer         s.append("Hello");         s.append(" ");         s.append("world");                	// String with the StringBuffer value       	String str = s.toString();         System.out.println(str);     } } 

Output
Hello world 


Advantages of using StringBuffer in Java

The advanatages of StringBuffer class are listed below:

  • Mutable: StringBuffer are mutable it means that we can change the content after the object has been created, on the other hand String are immutable once it created it can not be modified.
  • Efficient: Since StringBuffer objects are mutable, it is suitable in scenarios where we need to modify the string multiple times. If we do the same thing with string, everytime a new object is created and the old one is deleted, which is very bad in terms of performance and memory.

Note: Both String and StringBuffer objects are thread safe, but in different ways. StringBuffer is synchronized it means it is thread-safe but keep in mind that this synchronization can cause performance issues if accessed by multiple threads at the same time. On the other hand immutable objects like String are thread-safe because their state can not be modified once they are created.

Constructors of StringBuffer Class

Constructor

Description

Syntax

StringBuffer()

It reserves room for 16 characters without reallocation

StringBuffer s = new StringBuffer();

StringBuffer(int size)

It accepts an integer argument that explicitly sets the size of the buffer.

StringBuffer s = new StringBuffer(20);

StringBuffer(String str)

It accepts a string argument that sets the initial contents of the StringBuffer object and reserves room for 16 more characters without reallocation.

StringBuffer s = new StringBuffer("GeeksforGeeks");


Methods of Java StringBuffer Class

MethodsAction Performed
append()Used to add text at the end of the existing text.
length()The length of a StringBuffer can be found by the length( ) method.
capacity()the total allocated capacity can be found by the capacity( ) method.
charAt()This method returns the char value in this sequence at the specified index.
delete()Deletes a sequence of characters from the invoking object.
deleteCharAt()Deletes the character at the index specified by the loc.
ensureCapacity()Ensures capacity is at least equal to the given minimum.
insert()Inserts text at the specified index position.
length()Returns the length of the string.  
reverse()Reverse the characters within a StringBuffer object.
replace()Replace one set of characters with another set inside a StringBuffer object.


Examples of Java StringBuffer Method

1. append() Method

The append() method concatenates the given argument with this string.

Example: 

Java
import java.io.*;  class Geeks {     public static void main(String args[])     {         StringBuffer sb = new StringBuffer("Hello ");         sb.append("Java"); // now original string is changed         System.out.println(sb);     } } 

Output
Hello Java 


2. insert() Method

The insert() method inserts the given string with this string at the given position. 

Example:

Java
import java.io.*;  class Geeks {     public static void main(String args[])     {         StringBuffer sb = new StringBuffer("Hello ");         sb.insert(1, "Java");                  // Now original string is changed         System.out.println(sb);     } } 

Output
HJavaello  


3. replace() Method

The replace() method replaces the given string from the specified beginIndex and endIndex-1.

Example: 

Java
import java.io.*;  class Geeks {     public static void main(String args[]) {                StringBuffer sb = new StringBuffer("Hello");         sb.replace(1, 3, "Java");         System.out.println(sb);     } } 

Output
HJavalo 


4. delete() Method

The delete() method is used to delete the string from the specified beginIndex to endIndex-1.

Example:

Java
import java.io.*;  class Geeks {     public static void main(String args[]) {                StringBuffer sb = new StringBuffer("Hello");         sb.delete(1, 3);         System.out.println(sb);     } } 

Output
Hlo 


5. reverse() Method

The reverse() method of the StringBuffer class reverses the current string. 

Example: 

Java
import java.io.* ;  class Geeks {     public static void main(String args[]) {         StringBuffer sb = new StringBuffer("Hello");         sb.reverse();         System.out.println(sb);     } } 

Output
olleH 


6. capacity() Method

  • The capacity() method of the StringBuffer class returns the current capacity of the buffer. The default capacity of the buffer is 16. If the number of characters increases from its current capacity, it increases the capacity by (oldcapacity*2)+2.
  • For example, if the current capacity is 16, it will be (16*2)+2=34.

Example: 

Java
import java.io.*;  class Geeks {     public static void main(String args[])     {         StringBuffer sb = new StringBuffer();              	// default 16         System.out.println(sb.capacity());          sb.append("Hello");              	// now 16         System.out.println(sb.capacity());          sb.append("java is my favourite language");                	// (oldcapacity*2)+2       	System.out.println(sb.capacity());     } } 

Output
16 16 34 


Some Interesting Facts about the StringBuffer Class 

Do keep the following points in the back of your mind:  

  • java.lang.StringBuffer extends (or inherits from) Object class.
  • All Implemented Interfaces of StringBuffer classes are Serializable, Appendable, CharSequence.
  • public final class StringBuffer extends Object implements Serializable, CharSequence, Appendable.
  • String buffers are safe for use by multiple threads. The methods can be synchronized wherever necessary so that all the operations on any particular instance behave as if they occur in some serial order.
  • Whenever an operation occurs involving a source sequence (such as appending or inserting from a source sequence) this class synchronizes only on the string buffer performing the operation, not on the source.
  • It inherits some of the methods from the Object class which such as clone(), equals(), finalize(), getClass(), hashCode(), notifies(), notifyAll().

Remember: StringBuilder, J2SE 5 adds a new string class to Java’s already powerful string handling capabilities. This new class is called StringBuilder. It is identical to StringBuffer except for one important difference: it is not synchronized, which means that it is not thread-safe.

The advantage of StringBuilder is faster performance. However, in cases in which you are using multithreading,  you must use StringBuffer as it is thread-safe rather than StringBuilder.


Other Methods in Java StringBuffer

These auxiliary methods are as follows:

Methods

Description

Syntax

ensureCapacity()

It is used to increase the capacity of a StringBuffer object. The new capacity will be set to either the value we specify or twice the current capacity plus two (i.e. capacity+2), whichever is larger. Here, capacity specifies the size of the buffer.

void ensureCapacity(int capacity)

appendCodePoint(int codePoint)

This method appends the string representation of the codePoint argument to this sequence.

public StringBuffer appendCodePoint(int codePoint)

charAt(int index)

This method returns the char value in this sequence at the specified index.

public char charAt(int index)

IntStream chars()

This method returns a stream of int zero-extending the char values from this sequence.

public IntStream chars()

int codePointAt(int index)

This method returns the character (Unicode code point) at the specified index.

public int codePointAt(int index)

int codePointBefore(int index)

This method returns the character (Unicode code point) before the specified index.

public int codePointBefore(int index)

int codePointCount(int beginIndex, int endIndex)

This method returns the number of Unicode code points in the specified text range of this sequence.

public int codePointCount(int beginIndex, int endIndex)

IntStream codePoints()

This method returns a stream of code point values from this sequence.

public IntStream codePoints()

void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)

In this method, the characters are copied from this sequence into the destination character array dst.

public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)

int indexOf(String str)

This method returns the index within this string of the first occurrence of the specified substring.

public int indexOf(String str)
public int indexOf(String str, int fromIndex)

int lastIndexOf(String str)

This method returns the index within this string of the last occurrence of the specified substring.

public int lastIndexOf(String str)
public int lastIndexOf(String str, int fromIndex)

int offsetByCodePoints(int index, int codePointOffset)

This method returns the index within this sequence that is offset from the given index by codePointOffset code points.

public int offsetByCodePoints(int index, int codePointOffset)

void setCharAt(int index, char ch)

In this method, the character at the specified index is set to ch.

public void setCharAt(int index, char ch)

void setLength(int newLength)

This method sets the length of the character sequence.

public void setLength(int newLength)

CharSequence subSequence(int start, int end)

This method returns a new character sequence that is a subsequence of this sequence.

public CharSequence subSequence(int start, int end)

String substring(int start)

This method returns a new String that contains a subsequence of characters currently contained in this character sequence.

public String substring(int start)
public String substring(int start,int end)

String toString()

This method returns a string representing the data in this sequence.

public String toString()

void trimToSize()

This method attempts to reduce storage used for the character sequence.

public void trimToSize()

Note: Above we only have discussed the most widely used methods and do keep a tight bound around them as they are widely used in programming geeks.

Examples of the above Methods

Example 1: length() and capacity() Methods

Java
// Java Program to Illustrate StringBuffer class // via length() and capacity() methods import java.io.*;  class Geeks {     public static void main(String[] args) {          // Creating and storing string by creating object of         // StringBuffer         StringBuffer s = new StringBuffer("GeeksforGeeks");          // Getting the length of the string         int p = s.length();          // Getting the capacity of the string         int q = s.capacity();          // Printing the length and capacity of         // above generated input string on console         System.out.println("Length of string GeeksforGeeks="                            + p);         System.out.println("Capacity of string GeeksforGeeks="                            + q);     } } 

Output
Length of string GeeksforGeeks=13 Capacity of string GeeksforGeeks=29 


Example 2: append() 

It is used to add text at the end of the existing text. Here are a few of its forms: 

StringBuffer append(String str)
StringBuffer append(int num)

Illustration:

Java
// Java Program to Illustrate StringBuffer class // via append() method  import java.io.*;  class Geeks {   	public static void main(String[] args){                // Creating an object of StringBuffer class and         // passing random string         StringBuffer s = new StringBuffer("Geeksfor");          // Usage of append() method         s.append("Geeks");          System.out.println(s);          s.append(1);                  System.out.println(s);     } } 

Output
GeeksforGeeks GeeksforGeeks1 


Example 3: insert()

It is used to insert text at the specified index position. Syntax of method is mentioned below:

StringBuffer insert(int index, String str)
StringBuffer insert(int index, char ch)

Here, the index specifies the index at which point the string will be inserted into the invoking StringBuffer object.

Illustration:

Java
// Java Program to Illustrate StringBuffer class // via insert() method import java.io.*;  class Geeks {     public static void main(String[] args) {                // Creating an object of StringBuffer class         StringBuffer s = new StringBuffer("GeeksGeeks");          // Inserting element and position as an arguments         s.insert(5, "for");           System.out.println(s);          s.insert(0, 5);          System.out.println(s);          s.insert(3, true);          System.out.println(s);          s.insert(5, 41.35d);          System.out.println(s);          s.insert(8, 41.35f);           System.out.println(s);          // Declaring and initializing character array         char geeks_arr[] = { 'p', 'a', 'w', 'a', 'n' };          // Inserting character array at offset 9         s.insert(2, geeks_arr);          System.out.println(s);     } } 

Output
GeeksforGeeks 5GeeksforGeeks 5GetrueeksforGeeks 5Getr41.35ueeksforGeeks 5Getr41.41.3535ueeksforGeeks 5Gpawanetr41.41.3535ueeksforGeeks 


Example 4: reverse( )

It can reverse the characters within a StringBuffer object using reverse( ). This method returns the reversed object on which it was called. 

Java
// Java Program to Illustrate StringBuffer class // via reverse() method import java.io.*;  class Geeks {     public static void main(String[] args){                // Creating a string via creating         // object of StringBuffer class         StringBuffer s = new StringBuffer("GeeksGeeks");          // Invoking reverse() method         s.reverse();          System.out.println(s);     } } 

Output
skeeGskeeG 


Example 5: delete( ) and deleteCharAt()

It can delete characters within a StringBuffer by using the methods delete( ) and deleteCharAt( ). The delete( ) method deletes a sequence of characters from the invoking object. Here, the start Index specifies the index of the first character to remove, and the end Index specifies an index one past the last character to remove. Thus, the substring deleted runs from start Index to endIndex–1. The resulting StringBuffer object is returned. The deleteCharAt( ) method deletes the character at the index specified by loc. It returns the resulting StringBuffer object.

Syntax:

StringBuffer delete(int startIndex, int endIndex)
StringBuffer deleteCharAt(int loc)

Illustration:

Java
// Java Program to Illustrate StringBuffer class // via delete() and deleteCharAt() Methods  import java.io.*;  class Geeks {     public static void main(String[] args) {                StringBuffer s = new StringBuffer("GeeksforGeeks"); 	    s.delete(0, 5);             System.out.println(s);          s.deleteCharAt(7);          System.out.println(s);     } } 

Output
forGeeks forGeek 


Example 6: replace()

It can replace one set of characters with another set inside a StringBuffer object by calling replace( ). The substring being replaced is specified by the indexes start Index and endIndex. Thus, the substring at start Index through endIndex–1 is replaced. The replacement string is passed in str. The resulting StringBuffer object is returned.

Syntax:

StringBuffer replace(int startIndex, int endIndex, String str)

Illustration:

Java
// Java Program to Illustrate StringBuffer class // via replace() method import java.io.*;  class Geeks {     public static void main(String[] args) {                StringBuffer s = new StringBuffer("GeeksforGeeks");         s.replace(5, 8, "are");          System.out.println(s);     } } 

Output
GeeksareGeeks 

Next Article
Java StringBuilder Class

L

Lokesh Todwal
Improve
Article Tags :
  • Java
  • Java-Strings
  • Java-lang package
  • java-StringBuffer
Practice Tags :
  • Java
  • Java-Strings

Similar Reads

    Basics of Java

    Learn Java - A Beginners Guide for 2024
    If you are new to the world of coding and want to start your coding journey with Java, then this learn Java a beginners guide gives you a complete overview of how to start Java programming. Java is among the most popular and widely used programming languages and platforms. A platform is an environme
    10 min read
    Introduction to Java
    Java is a high-level, object-oriented programming language developed by Sun Microsystems in 1995. It is platform-independent, which means we can write code once and run it anywhere using the Java Virtual Machine (JVM). Java is mostly used for building desktop applications, web applications, Android
    4 min read
    Similarities and Difference between Java and C++
    Nowadays Java and C++ programming languages are vastly used in competitive coding. Due to some awesome features, these two programming languages are widely used in industries as well as competitive programming. C++ is a widely popular language among coders for its efficiency, high speed, and dynamic
    6 min read
    Setting up Environment Variables For Java - Complete Guide to Set JAVA_HOME
    In the journey to learning the Java programming language, setting up environment variables for Java is essential because it helps the system locate the Java tools needed to run the Java programs. Now, this guide on how to setting up environment variables for Java is a one-place solution for Mac, Win
    6 min read
    Java Syntax
    Java is an object-oriented programming language that is known for its simplicity, portability, and robustness. The syntax of Java programming language is very closely aligned with C and C++, which makes it easier to understand. Java Syntax refers to a set of rules that define how Java programs are w
    6 min read
    Java Hello World Program
    Java is one of the most popular and widely used programming languages and platforms. In this article, we will learn how to write a simple Java Program. This article will guide you on how to write, compile, and run your first Java program. With the help of Java, we can develop web and mobile applicat
    6 min read
    Differences Between JDK, JRE and JVM
    Understanding the difference between JDK, JRE, and JVM plays a very important role in understanding how Java works and how each component contributes to the development and execution of Java applications. The main difference between JDK, JRE, and JVM is:JDK: Java Development Kit is a software develo
    3 min read
    How JVM Works - JVM Architecture
    JVM (Java Virtual Machine) runs Java applications as a run-time engine. JVM is the one that calls the main method present in a Java code. JVM is a part of JRE (Java Runtime Environment). Java applications are called WORA (Write Once Run Anywhere). This means a programmer can develop Java code on one
    7 min read
    Java Identifiers
    An identifier in Java is the name given to Variables, Classes, Methods, Packages, Interfaces, etc. These are the unique names used to identify programming elements. Every Java Variable must be identified with a unique name.Example:public class Test{ public static void main(String[] args) { int a = 2
    2 min read

    Variables & DataTypes in Java

    Java Variables
    In Java, variables are containers that store data in memory. Understanding variables plays a very important role as it defines how data is stored, accessed, and manipulated.Key Components of Variables in Java:A variable in Java has three components, which are listed below:Data Type: Defines the kind
    9 min read
    Scope of Variables in Java
    The scope of variables is the part of the program where the variable is accessible. Like C/C++, in Java, all identifiers are lexically (or statically) scoped, i.e., scope of a variable can be determined at compile time and independent of the function call stack. In this article, we will learn about
    7 min read
    Java Data Types
    Java is statically typed and also a strongly typed language because each type of data, such as integer, character, hexadecimal, packed decimal etc. is predefined as part of the programming language, and all constants or variables defined for a given program must be declared with the specific data ty
    14 min read

    Operators in Java

    Java Operators
    Java operators are special symbols that perform operations on variables or values. These operators are essential in programming as they allow you to manipulate data efficiently. They can be classified into different categories based on their functionality. In this article, we will explore different
    15 min read
    Java Arithmetic Operators with Examples
    Operators constitute the basic building block to any programming language. Java too provides many types of operators which can be used according to the need to perform various calculations and functions, be it logical, arithmetic, relational, etc. They are classified based on the functionality they
    6 min read
    Java Assignment Operators with Examples
    Operators constitute the basic building block of any programming language. Java too provides many types of operators which can be used according to the need to perform various calculations and functions, be it logical, arithmetic, relational, etc. They are classified based on the functionality they
    7 min read
    Java Unary Operator with Examples
    Operators constitute the basic building block to any programming language. Java too provides many types of operators which can be used according to the need to perform various calculations and functions be it logical, arithmetic, relational, etc. They are classified based on the functionality they p
    8 min read
    Java Relational Operators with Examples
    Operators constitute the basic building block to any programming language. Java too provides many types of operators which can be used according to the need to perform various calculations and functions, be it logical, arithmetic, relational, etc. They are classified based on the functionality they
    10 min read
    Java Logical Operators with Examples
    Logical operators are used to perform logical "AND", "OR", and "NOT" operations, i.e., the functions similar to AND gate and OR gate in digital electronics. They are used to combine two or more conditions/constraints or to complement the evaluation of the original condition under particular consider
    8 min read
    Java Ternary Operator
    Operators constitute the basic building block of any programming language. Java provides many types of operators that can be used according to the need to perform various calculations and functions, be it logical, arithmetic, relational, etc. They are classified based on the functionality they provi
    5 min read
    Bitwise Operators in Java
    In Java, Operators are special symbols that perform specific operations on one or more than one operands. They build the foundation for any type of calculation or logic in programming.There are so many operators in Java, among all, bitwise operators are used to perform operations at the bit level. T
    6 min read

    Packages in Java

    Java Packages
    Packages in Java are a mechanism that encapsulates a group of classes, sub-packages, and interfaces. Packages are used for: Prevent naming conflicts by allowing classes with the same name to exist in different packages, like college.staff.cse.Employee and college.staff.ee.Employee.They make it easie
    8 min read

    Flow Control in Java

    Decision Making in Java (if, if-else, switch, break, continue, jump)
    Decision-making statements in Java execute a block of code based on a condition. Decision-making in programming is similar to decision-making in real life. In programming, we also face situations where we want a certain block of code to be executed when some condition is fulfilled.A programming lang
    10 min read
    Java if statement
    The Java if statement is the most simple decision-making statement. It is used to decide whether a certain statement or block of statements will be executed or not i.e. if a certain condition is true then a block of statements is executed otherwise not.Example:Java// Java program to illustrate If st
    5 min read
    Java if-else Statement
    The if-else statement in Java is a powerful decision-making tool used to control the program's flow based on conditions. It executes one block of code if a condition is true and another block if the condition is false. In this article, we will learn Java if-else statement with examples.Example:Java/
    3 min read
    Java if-else-if ladder with Examples
    The Java if-else-if ladder is used to evaluate multiple conditions sequentially. It allows a program to check several conditions and execute the block of code associated with the first true condition. If none of the conditions are true, an optional else block can execute as a fallback.Example: The b
    3 min read

    Loops in Java

    Java Loops
    Looping in programming languages is a feature that facilitates the execution of a set of instructions repeatedly while some condition evaluates to true. Java provides three ways for executing the loops. While all the ways provide similar basic functionality, they differ in their syntax and condition
    7 min read
    Java For Loop
    Java for loop is a control flow statement that allows code to be executed repeatedly based on a given condition. The for loop in Java provides an efficient way to iterate over a range of values, execute code multiple times, or traverse arrays and collections.Now let's go through a simple Java for lo
    4 min read
    Java while Loop
    Java while loop is a control flow statement used to execute the block of statements repeatedly until the given condition evaluates to false. Once the condition becomes false, the line immediately after the loop in the program is executed.Let's go through a simple example of a Java while loop:Javapub
    3 min read
    Java Do While Loop
    Java do-while loop is an Exit control loop. Unlike for or while loop, a do-while check for the condition after executing the statements of the loop body.Example:Java// Java program to show the use of do while loop public class GFG { public static void main(String[] args) { int c = 1; // Using do-whi
    4 min read
    For-Each Loop in Java
    The for-each loop in Java (also called the enhanced for loop) was introduced in Java 5 to simplify iteration over arrays and collections. It is cleaner and more readable than the traditional for loop and is commonly used when the exact index of an element is not required.Example: Using a for-each lo
    8 min read

    Jump Statements in Java

    Java Continue Statement
    In Java, the continue statement is used inside the loops such as for, while, and do-while to skip the current iteration and move directly to the next iteration of the loop.Example:Java// Java Program to illustrate the use of continue statement public class Geeks { public static void main(String args
    4 min read
    Java Break Statement
    The Break Statement in Java is a control flow statement used to terminate loops and switch cases. As soon as the break statement is encountered from within a loop, the loop iterations stop there, and control returns from the loop immediately to the first statement after the loop. Example:Java// Java
    3 min read
    Java return Keyword
    return keyword in Java is a reserved keyword which is used to exit from a method, with or without a value. The usage of the return keyword can be categorized into two cases:Methods returning a valueMethods not returning a value1. Methods Returning a ValueFor the methods that define a return type, th
    4 min read

    Arrays in Java

    Arrays in Java
    Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
    15+ min read
    Java Multi-Dimensional Arrays
    Multidimensional arrays are used to store the data in rows and columns, where each row can represent another individual array are multidimensional array. It is also known as array of arrays. The multidimensional array has more than one dimension, where each row is stored in the heap independently. T
    10 min read
    Jagged Array in Java
    In Java, a Jagged array is an array that holds other arrays. When we work with a jagged array, one thing to keep in mind is that the inner array can be of different lengths. It is like a 2D array, but each row can have a different number of elements.Example:arr [][]= { {10,20}, {30,40,50,60},{70,80,
    6 min read

    Strings in Java

    Java Strings
    In Java, a String is the type of object that can store a sequence of characters enclosed by double quotes, and every character is stored in 16 bits, i.e., using UTF 16-bit encoding. A string acts the same as an array of characters. Java provides a robust and flexible API for handling strings, allowi
    9 min read
    String Class in Java
    A string is a sequence of characters. In Java, objects of the String class are immutable, which means they cannot be changed once created. In this article, we are going to learn about the String class in Java.Example of String Class in Java:Java// Java Program to Create a String import java.io.*; cl
    7 min read
    StringBuffer Class in Java
    The StringBuffer class in Java represents a sequence of characters that can be modified, which means we can change the content of the StringBuffer without creating a new object every time. It represents a mutable sequence of characters.Features of StringBuffer ClassThe key features of StringBuffer c
    11 min read
    Java StringBuilder Class
    In Java, the StringBuilder class is a part of the java.lang package that provides a mutable sequence of characters. Unlike String (which is immutable), StringBuilder allows in-place modifications, making it memory-efficient and faster for frequent string operations.Declaration:StringBuilder sb = new
    7 min read

    OOPS in Java

    Java OOP(Object Oriented Programming) Concepts
    Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
    13 min read
    Classes and Objects in Java
    In Java, classes and objects are basic concepts of Object Oriented Programming (OOPs) that are used to represent real-world concepts and entities. The class represents a group of objects having similar properties and behavior, or in other words, we can say that a class is a blueprint for objects, wh
    11 min read
    Java Methods
    Java Methods are blocks of code that perform a specific task. A method allows us to reuse code, improving both efficiency and organization. All methods in Java must belong to a class. Methods are similar to functions and expose the behavior of objects.Example: Java program to demonstrate how to crea
    8 min read
    Access Modifiers in Java
    In Java, access modifiers are essential tools that define how the members of a class, like variables, methods, and even the class itself can be accessed from other parts of our program. They are an important part of building secure and modular code when designing large applications. Understanding de
    7 min read
    Wrapper Classes in Java
    A Wrapper class in Java is one whose object wraps or contains primitive data types. When we create an object in a wrapper class, it contains a field, and in this field, we can store primitive data types. In other words, we can wrap a primitive value into a wrapper class object. Let's check on the wr
    6 min read
    Need of Wrapper Classes in Java
    Firstly the question that hits the programmers is when we have primitive data types then why does there arise a need for the concept of wrapper classes in java. It is because of the additional features being there in the Wrapper class over the primitive data types when it comes to usage. These metho
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences