Strings

Strings are a primitive data-type in many programming languages. In Java a String is an object because it can be represented as an array of the char primitive type. For instance, the String  "hello" could be represented as follows:

char[] s1 = {'h','e','l','l','o'};

Obviously this is rather painful, and is why Java supports Strings as a built-in type:

String s1 = "hello";

Strings in Java are immutable: once they are constructed they can never be changed. This may sound implausible, given the following valid lines of Java:

String s1 = "Hello";

s1 = s1 + " World";

Although it is a subtle point (and will be explained in great detail in the chapters to come), no Strings have been modified in this example. The only thing changing in this example is the underlying String that the variable s1 refers to.

The variable s1 starts out referring to a String object with the value "Hello". On the second line, Java first constructs a new String object with the value " World". Next, when the concatenation is performed, Java creates a third String object with the value "Hello World", and points the variable s1 at this.

Because Strings are objects, they support methods. It is important to realize that although these methods perform operations with the value of the String, due to the fact that Strings are immutable, they never change the value of the String – instead they construct new Strings:

package strings;

public class Strings {

    public static void main(String[] args) {

        String s1 = "Hello World";

        System.out.println(s1.toUpperCase());

        System.out.println(s1.substring(6));

        System.out.println(s1.indexOf("W"));

    }

}

The program above prints out:

HELLO WORLD

World

6

Some of the more useful methods supported by String are as follows:

  • length: this returns the number of characters in the String

  • matches: this tests whether the String matches a given regular expression. Regular expressions are outside the scope of this book, but are a standard way of expressing textual patterns

  • replaceAll and replaceFirst: replace instances of a specified String with a new String

  • split: splits the String based on a delimiter (for example, a comma) and returns an array of Strings

  • substring: returns a portion of the String based on a starting and (optionally) end index. As with arrays, the first character in the String is at position 0

  • trim: removes leading and trailing whitespace from the String

StringBuilders are similar to Strings, except they are mutable: A StringBuilder is an object, and encapsulates a string of text, but this string can be modified after the StringBuilder has been created:

package strings;

public class StringBuilder {

    public static void main(String[] args) {

        StringBuilder sb1 = new StringBuilder ("Hello");

        sb1.append(" World!!!");

        sb1.reverse();

        System.out.println(sb1);

    }

}

This produces:

!!!dlroW olleH

The main reason StringBuilders are used is due to their performance advantage over Strings when concatenating many values together. String concatenation is a relatively common task, and there are cases when hundreds or thousands of strings must be concatenated together. In these cases the append method on StringBuilder will generally offer superior performance to string concatenation using the + operator.

There is a similar class to StringBuilder called StringBuffer. These two classes are essentially the same except StringBuilder is not thread safe. Do not worry if you do not understand that concept yet, it will be explained in the Threading chapter. As a general rule StringBuilder offers superior performance to StringBuffer.

Unless I am concatenating a large number of strings together I generally prefer the simplicity of Strings. The speed of modern computers means it is seldom worth optimizing relatively simple operations when there is a trade-off in terms of code simplicity.

A Software Engineer Learns Java and Object Orientated Programming
titlepage.xhtml
part0000_split_000.html
part0000_split_001.html
part0000_split_002.html
part0000_split_003.html
part0000_split_004.html
part0000_split_005.html
part0000_split_006.html
part0000_split_007.html
part0000_split_008.html
part0000_split_009.html
part0000_split_010.html
part0000_split_011.html
part0000_split_012.html
part0000_split_013.html
part0000_split_014.html
part0000_split_015.html
part0000_split_016.html
part0000_split_017.html
part0000_split_018.html
part0000_split_019.html
part0000_split_020.html
part0000_split_021.html
part0000_split_022.html
part0000_split_023.html
part0000_split_024.html
part0000_split_025.html
part0000_split_026.html
part0000_split_027.html
part0000_split_028.html
part0000_split_029.html
part0000_split_030.html
part0000_split_031.html
part0000_split_032.html
part0000_split_033.html
part0000_split_034.html
part0000_split_035.html
part0000_split_036.html
part0000_split_037.html
part0000_split_038.html
part0000_split_039.html
part0000_split_040.html
part0000_split_041.html
part0000_split_042.html
part0000_split_043.html
part0000_split_044.html
part0000_split_045.html
part0000_split_046.html
part0000_split_047.html
part0000_split_048.html
part0000_split_049.html
part0000_split_050.html
part0000_split_051.html
part0000_split_052.html
part0000_split_053.html
part0000_split_054.html
part0000_split_055.html
part0000_split_056.html
part0000_split_057.html
part0000_split_058.html
part0000_split_059.html
part0000_split_060.html
part0000_split_061.html
part0000_split_062.html
part0000_split_063.html
part0000_split_064.html
part0000_split_065.html
part0000_split_066.html
part0000_split_067.html
part0000_split_068.html
part0000_split_069.html
part0000_split_070.html
part0000_split_071.html
part0000_split_072.html
part0000_split_073.html
part0000_split_074.html
part0000_split_075.html
part0000_split_076.html
part0000_split_077.html
part0000_split_078.html
part0000_split_079.html
part0000_split_080.html
part0000_split_081.html
part0000_split_082.html
part0000_split_083.html
part0000_split_084.html
part0000_split_085.html
part0000_split_086.html
part0000_split_087.html
part0000_split_088.html
part0000_split_089.html
part0000_split_090.html
part0000_split_091.html
part0000_split_092.html
part0000_split_093.html
part0000_split_094.html
part0000_split_095.html
part0000_split_096.html
part0000_split_097.html
part0000_split_098.html
part0000_split_099.html
part0000_split_100.html
part0000_split_101.html
part0000_split_102.html
part0000_split_103.html
part0000_split_104.html
part0000_split_105.html
part0000_split_106.html
part0000_split_107.html
part0000_split_108.html
part0000_split_109.html
part0000_split_110.html
part0000_split_111.html
part0000_split_112.html
part0000_split_113.html
part0000_split_114.html
part0000_split_115.html
part0000_split_116.html
part0000_split_117.html
part0000_split_118.html
part0000_split_119.html
part0000_split_120.html
part0000_split_121.html
part0000_split_122.html
part0000_split_123.html
part0000_split_124.html
part0000_split_125.html
part0000_split_126.html
part0000_split_127.html
part0000_split_128.html
part0000_split_129.html
part0000_split_130.html
part0000_split_131.html
part0000_split_132.html
part0000_split_133.html
part0000_split_134.html
part0000_split_135.html
part0000_split_136.html
part0000_split_137.html
part0000_split_138.html
part0000_split_139.html