Learn-dsa..in 30 days!



























String Basics

Strings represent sequences of characters. Strings are not primitive data types like int or char, but a derived data type. The Java String class provides methods like compare(), concat(), equals(), split(), length(), replace(), compareTo(), intern(), substring(). Strings in Java are unchangeable once created, that is Java Strings are immutable and final.

Use cases for Strings

Strings are used processing, manipulating and formatting text.
Encoding/decoding and serialization deserialization can be done using Strings.
Strings are used in File operations such as reading, writing, modification etc.
Search engines use String data matching algorithms.

Advantages of Strings

Java Strings are easy to create and manipulate (via API methods like search, replace etc.).
Java Strings support internationalization, date formats, currency formats etc.
Strings concepts are mostly same across programming languages making them a universal data structure.

Disadvantages of Strings

Strings are immutable, which means deletion or addition of characters to a String requires creation of a new String.
Strings have a performance overhead over other primitive data types like char array etc.
Large Strings consume lot of memory in operations like search replace etc.

Time Complexity of Java String class operations:

Following table shows time complexity metrics for an String with n characters:

Operation Complexity
Accessing a character of the String when index is known. O(1)
Search character/substring in String when index is unknown. O(n)
Finding String length. O(1)
Creating a copy of a String. O(n)