Introduction to Strings
Explore the fundamentals of strings as ordered sequences of characters in Java and how they differ from general arrays. Understand the nature of characters, how strings are stored in memory, and their significance in problems like pattern matching and text processing. This lesson equips you to recognize strings' properties and prepares you for string manipulation and algorithmic challenges.
By this point, you are familiar with arrays as ordered collections of elements stored by position. Arrays provide a general way to store sequences of values such as integers, floating-point numbers, and objects. Each element in an array can be accessed efficiently using its index.
However, many important computational problems involve textual and symbolic data rather than only numeric values. Names, passwords, file paths, search queries, source code, and biological sequences are all examples of data that must be handled as sequences of characters. Although arrays can store characters, viewing such data only as a general array is often not enough for algorithmic study.
Character-based data gives rise to a different kind of problem. In numerical arrays, common tasks include searching for values, computing sums, or rearranging elements. In character sequences, the focus shifts to questions such as whether two words are equal, whether a pattern appears inside a larger text, how often a character occurs, whether a string is a palindrome, and how two strings compare.
Strings provide a natural way to represent this type of data. A string is an ordered sequence of characters, and it forms the basis of many important computational tasks such as text processing, pattern matching, and validation.
What is a character?
Before defining a string precisely, we need to be clear about what a string is made of. The basic building block of every string is a character.
A character is a single symbol. It can be a letter like 'a' or 'Z', a digit like '3' or '9', a space, a punctuation mark like '?' or '.', or a special symbol like '@' or '#'. Anything you can type on a keyboard is a character. Each character represents one unit of textual information, and it occupies exactly one position in memory.
It is important to distinguish a character from a string early on because they are two different things, even though they can look similar when written in code.
char ch = 'h'; // a single characterString s = "hello"; // a String made up of 5 characters
A single character is the smallest possible unit. A string is a collection of those units arranged in a specific order. You cannot ...