0% found this document useful (0 votes)
1 views3 pages

Java Practical Strings

This document compares the String and StringBuffer classes in Java, highlighting their methods and key differences. The String class is immutable, while StringBuffer is mutable, allowing for modifications. Additionally, StringBuffer is more memory efficient and faster for frequent modifications compared to String.

Uploaded by

op922697
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)
1 views3 pages

Java Practical Strings

This document compares the String and StringBuffer classes in Java, highlighting their methods and key differences. The String class is immutable, while StringBuffer is mutable, allowing for modifications. Additionally, StringBuffer is more memory efficient and faster for frequent modifications compared to String.

Uploaded by

op922697
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

Practical Assignment: Java String

Classes

This document explores the implementation and differences between the


String and StringBuffer classes in Java.

1. Methods of the String Class

The String class is used to create immutable character strings. Once a String
object is created, its value cannot be changed.

public class StringMethods {


public static void main(String[] args) {
String str = "Computer Engineering";

// length() - returns the number of characters


[Link]("Length: " + [Link]());

// charAt() - returns the character at a specific index


[Link]("Character at index 5: " + [Link](5));

// substring() - extracts a part of the string


[Link]("Substring (index 0-8): " +
[Link](0, 8));

// toUpperCase() - converts to capital letters


[Link]("Uppercase: " + [Link]());

// contains() - checks if a sequence exists in the string


[Link]("Contains 'Eng': " + [Link]("Eng"));
}
}

Page 1
2. Methods of the StringBuffer Class

The StringBuffer class is used to create mutable strings, allowing for


modifications without creating new objects.

public class StringBufferMethods {


public static void main(String[] args) {
StringBuffer sb = new StringBuffer("Java");

// append() - adds text to the end


[Link](" Practical");
[Link]("After append: " + sb);

// insert() - inserts text at a specific position


[Link](4, " Programming");
[Link]("After insert: " + sb);

// replace() - replaces a part of the string


[Link](0, 4, "Python");
[Link]("After replace: " + sb);

// reverse() - reverses the string sequence


[Link]();
[Link]("After reverse: " + sb);

// delete() - deletes characters between indices


[Link](0, 5);
[Link]("After delete: " + sb);
}
}

Summary of Key Differences

Feature String StringBuffer

Immutable (cannot be
Mutability Mutable (can be changed)
changed)

Page 2
Feature String StringBuffer

Slower when performing Faster for frequent


Performance
many modifications modifications

Uses more memory if More memory efficient


Memory
strings change often for modifications

Page 3

You might also like