Skip to main content

Unit 6 String in Java


1. Introduction to String

In Java, a String is an object that represents a sequence of characters.
Strings are written using double quotation marks (" ").

Example:

String s = "Java";

A character array can also be used to create a String.

Example:

char[] ch = {'j','a','v','a'};
String s = new String(ch);

This is the same as:

String s = "java";

 Java StringBuffer Class

The StringBuffer class is used to create mutable (modifiable) string objects.

Unlike the String class, StringBuffer objects can be modified after creation.

·         String → Immutable (cannot change)

·         StringBuffer → Mutable (can change)

Features of StringBuffer

·         Characters can be inserted, replaced, or deleted

·         Automatically grows when more characters are added

·         Stores data in heap memory

·         Thread-safe (synchronized)

Important Constructors of StringBuffer

Constructor

Description

StringBuffer()

Creates an empty StringBuffer with initial capacity 16

StringBuffer(String str)

Creates a StringBuffer with the given string

StringBuffer(int capacity)

Creates an empty StringBuffer with specified capacity

 Java StringBuilder Class

The StringBuilder class is also used to create mutable strings.

It is similar to StringBuffer, but it is non-synchronized, meaning it is faster but not thread-safe.

It was introduced in JDK 1.5.

Important Constructors of StringBuilder

Constructor

Description

StringBuilder()

Creates an empty StringBuilder with capacity 16

StringBuilder(String str)

Creates a StringBuilder with the specified string

StringBuilder(int capacity)

Creates an empty StringBuilder with given capacity

 StringBuffer Methods

1. append() Method

The append() method adds a string at the end of the existing string.

Example:

class StringBufferExample {
    public static void main(String args[]) {
        StringBuffer sb = new StringBuffer("Hello ");
        sb.append("Java");   // original string changes
        System.out.println(sb);
    }
}

Output

Hello Java

 insert() Method

The insert() method inserts a string at a specified position.

Example:

class StringBufferExample2 {
    public static void main(String args[]) {
        StringBuffer sb = new StringBuffer("Hello ");
        sb.insert(1, "Java");
        System.out.println(sb);
    }
}

Output

HJavaello

 

3. replace() Method

The replace() method replaces characters between the specified index positions.

Example:

class StringBufferExample3 {
    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 removes characters between specified indexes.

Example:

class StringBufferExample4 {
    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 reverses the string.

Example:

class StringBufferExample5 {
    public static void main(String args[]) {
        StringBuffer sb = new StringBuffer("Hello");
        sb.reverse();
        System.out.println(sb);
    }
}

Output

olleH

 Difference Between String and StringBuffer

No

String

StringBuffer

1

Immutable (cannot be changed)

Mutable (can be changed)

2

Creates new object when modified

Modifies existing object

3

Slower for concatenation

Faster for concatenation

4

Overrides equals() method

Does not override equals()

 

 Java String length() Method

The length() method returns the number of characters in a string.

Syntax

public int length()

Example:

class CalcLength {
    public static void main(String args[]) {
        String name = "Santosh";
        int length = name.length();

        System.out.println("The length of the String \"" + name + "\" is: " + length);
    }
}

Output

The length of the String "Santosh" is: 7

7. Java String concat() Method

The concat() method joins two or more strings together.

Syntax

string1.concat(string2)

Example:

public class ConcatExample {
    public static void main(String[] args) {

        String str1 = "Hello ";
        String str2 = "Ramesh ";
        String str3 = "Sharma";

        String str4 = str1.concat(str2);
        System.out.println(str4);

        String str5 = str1.concat(str2).concat(str3);
        System.out.println(str5);
    }
}

Output

Hello Ramesh
Hello Ramesh Sharma

 

Comments

Popular posts from this blog

Function of OS 1. Process Management The operating system helps in running many programs at the same time. It keeps track of each running program (called a process), decides which one should run next, and stops or starts them as needed. It makes sure that all the programs get a fair chance to use the CPU.   2. Memory Management The OS manages the computer's memory (RAM). It decides which program will use how much memory and keeps track of it. When a program is closed, it frees up the memory so that other programs can use it. This helps the computer run smoothly without crashing.   3. File System Management The operating system helps us to create, save, open, and delete files. It organizes files in folders and keeps them safe. It also controls who can open or edit a file to protect our data.   4. Device Management The OS controls all the input and output devices like the keyboard, mouse, printer, and monitor. It tells the devices what to do and makes su...

UNIT 3 Array in C#

  Array in C# An array is a collection of variables of the same data type , stored in contiguous memory locations , and accessed using a common name and index . Each item in an array is called an element , and the index of arrays in C# starts from 0 .     Key Points About Arrays in C#: 1.       Elements are stored in contiguous memory locations. 2.       Index starts from 0. So, for an array of size 5, valid indexes are 0 to 4. 3.       Arrays are reference types and are allocated on the heap. 4.       C# array is an object of base type System.Array . 5.       Array elements can be of any type , including another array (array of arrays). 6.       Jagged array (array of arrays) elements are reference types and are initialized to null . 7.       Arrays can be single-dimensi...

Unit 5 SEP

  Software analysis and Design tools Software Analysis and Design Tools are special computer programs that help developers and designers in every stage of making software. These tools make it easier to understand, plan, design, and document a software system so that the final product works well and meets user needs. They help in collecting requirements by recording what users want, modeling the system using diagrams such as flowcharts and UML, and designing the structure and interface of the software. These tools also help in creating documentation for clear communication, analyzing the design to find and fix problems early, and supporting teamwork by allowing different people to work together on the same project smoothly. Introduction of ER Model The Entity-Relationship (ER) Model is a conceptual model used to design and represent the logical structure of a database . It shows entities, their attributes, and relationships among them. Example: A Student enr...