You are currently viewing String Class In Java: Essential Techniques for Text Manipulation

String Class In Java: Essential Techniques for Text Manipulation

1. Introduction

The String class in Java is used to represent text data. In this tutorial, you’ll learn the internal representation of strings, how to create them, and some common operations.

Affiliate Disclosure
This post contains affiliate links. If you purchase through these links, I may earn a small commission at no additional cost to you. As an Amazon Associate, I earn from qualifying purchases.

2. Internal Representation of Strings

In Java, a String is not a primitive type, but an object type. In its internal representation, a String is an array of characters. The machine representation of a character depends on the encoding used. The following formats are the most commonly encountered:

  • LATIN-1: Also known as ISO 8859-1. This format uses a one-byte fixed-length scheme to represent each character. A character is therefore represented on 8 bits. This encoding can be used to represent most Western European languages.
  • UTF-16: This encoding scheme is a variable-length scheme that uses either 2 bytes or 4 bytes to encode each character. With this encoding format, you can represent all the languages in the world. You should know that Java only uses 2 bytes to represent a UTF-16 character, that is any value from the range 0000 to FFFF.

When representing a String, if all characters can be stored in a single byte, then the LATIN-1 encoding scheme is used. Otherwise, if at least one character required two bytes to be stored, the UTF-16 scheme is used instead.

3. Creating and Initializing Strings

3.1. String class is Immutable

Before talking about string creation, there’s an important concept we need to mention: the immutability of the String class. The String class is immutable, meaning that once a string object has been created, it can no longer be directly modified. Any modification returns a new object.

There are two main mechanisms for creating strings in Java. The first is by using the string literal and the second is by using the constructors of the String class through the new operator.

3.2. Creating strings using the String Literal

This is the simplest way to create a String. You should enclose your text in double quotes like this:

String myString = “This is a string created using double-quotes”

When you create a string in this way, the compiler saves it in a dedicated memory location to optimize memory use. That memory location is called the String pool. If a string with the same value as the one you want to create already exists in the “String pool”, a reference to it is returned. Otherwise, a new String is created in the String pool. Strings that are stored in the String pool are said to be interned.

3.3. Creating strings using Constructors

The String class has a wide range of constructors than can be used to create strings based on various object types. Find below some of the most used ones:

new String() : This will create an empty String.

new String(String str): Creates a string object from another String. The other String (str) can be a string literal or another String object.

new String(char[] chars): Creates a string from an array of characters.

String Creation Examples

        String str1 = new String();
        String str2 = new String("John Doe");
        String str3 = new String (str2);
        char[] chars = {'J','o','h','n',' ','D','o','e'};
        String str4 = new String(chars);
        System.out.println(str1);//empty string
        System.out.println(str2);//John Doe
        System.out.println(str3);//John Doe
        System.out.println(str4);//John Doe

Note that when you create a String using the constructor, the string is not interned. If you need to explicitly intern your String, there is a special method for that:

String intern(): This method will check if there is already an equivalent String in the String pool and returns a reference to that string. If there is no such string, then a new string is created in the String pool and its reference is returned.

        String str1 = "John Doe";
        String str2 = new String("John Doe");
        String str3 = str2.intern();
        System.out.println(str1 == str2);//false, since they are two different objects
        System.out.println(str1 == str3);//true, since they refer to the same object in the String pool

4. Operating on Strings

Next, let’s have a look at a few operations that you can perform on string objects. The “String” class offers a wide variety of methods for different use cases. Here, we focus on a few of the most common use cases.

4.1. Obtaining the Length of a String

You may obtain the length of any String object by using the following method:

int length();

length() Example

       String myString = "Hello World!";
       System.out.println(myString.length());// 12

4.2. Reading Characters from a String

You can use the following methods for character-related operations in Java:

char charAt(int index) : Returns the character at the given index

charAt() Example

       String myString = "Hello World!";
       System.out.println(myString.charAt(0));// H

char[] toCharArray() : Returns an array containing the characters of the underlying string

toCharArray() Example

       String myString = "Hello World!";
       char[] array = myString.toCharArray();
       System.out.println(array[0]);// H

boolean isEmpty() : Returns true if the underlying array is empty and false otherwise

isEmpty() Example

        String notEmpty = "Hello World!";
        String empty = "";
        System.out.println(notEmpty.isEmpty());// false
        System.out.println(empty.isEmpty());// true

IntStream chars() : Returns a Stream of characters of the underlying string.

chars() Example

        String myString = "Hello World!";
        IntStream chars = myString.chars();
        chars.mapToObj(c ->(char)c).forEach(System.out::print);// Hello World!

4.3. Reading Lines from a String

Use the following method to extract lines from a given string object:

Streamlines(): Returns a stream of lines extracted from the given string object, separated by a line terminator.The line terminator can be any of the following characters: a line feed character “\n”, a carriage return “\r”, or a carriage return followed by a line feed “\r\n”.

lines() Example

        String multiLine = "Hello \nWorld!";
        Stream<String> lines = multiLine.lines();
        lines.forEach(System.out::println);//1st line: Hello; 2nd line: World!

4.4. Comparing Strings

Two strings are compared character by character, like in a dictionary. The comparison of two characters is based on their “Unicode” code.

        boolean test = 'a' < 'b'; //true since U+0061 < U+0062

Java offers several methods to simplify string comparison:

boolean equals(Object object): The String class overrides this method from the Object class, performing a character-by-character lexicology comparison. This method returns true if and only if all the characters of the two objects are equals (by their Unicode Code).

equals() Example

        String firstString = "John";
        String secondString = "John";
        boolean test = firstString.equals(secondString);
        System.out.println(test);//true

boolean equalsIgnoreCase(String str2): This method does the same thing as the equals() method to the difference that it does not consider the case of the characters.

equalsIgnoreCase() Example

        String firstString = "John";
        String secondString = "john";
        boolean test = firstString.equalsIgnoreCase(secondString);
        System.out.println(test);//true

int compareTo(String str2): The String class implements the Comparable interface. This method compares two strings and returns an integer whose value depends on the result of the comparison:

  • The value 0, if the two strings are equals
  • A positive value, if the underlying string is greater than the string argument
  • A negative value, if the underlying string is less than the string argument

compareTo() Example

        String firstString = "John";
        String secondString = "Doe";
        int test = firstString.compareTo(secondString);
        System.out.println(test);//A positive value

4.5. Concatenating Strings

Concatenating is the process of adding a string to another. You can use the concat method of the String class to concatenate two strings. Since the String class is immutable, this method returns a new object.

String concat(String str2): Concatenates “str2” to the end of the underlying string. If “str2” is empty, then the current String object is returned. Otherwise, a new String object is returned with the value of the concatenated strings.

concat() Example

        String firstString = "John ";
        String secondString = "Doe";
        String concatenatedString = firstString.concat(secondString);
        System.out.println(concatenatedString);//John Doe

You can also concatenate two strings by using the “+” operator. That operator is overloaded for strings and performs a concatenation.

        String firstString = "John ";
        String secondString = "Doe";
        String concatenatedString = firstString + secondString;
        System.out.println(concatenatedString);//John Doe

We’ve just scratched the surface of string manipulation in Java. If you want to learn more about the String class, check out the official documentation.

5. Conclusion

In this tutorial, you learned about the internal representation of strings in Java. You also learned how to create strings and perform common operations on them. If you want to learn more useful tips on String manipulation, check out this article about String manipulation for Technical Interviews.

6. References

1) OCP Oracle Certified Professional Java SE 17 by Khalil A. Mughal and Vasily A. Strelnikov
2) Oracle Java Documentation

Noel Kamphoa

Experienced software engineer with expertise in Telecom, Payroll, and Banking. Now Senior Software Engineer at Societe Generale Paris.

This Post Has 2 Comments

Leave a Reply