Java: An introduction to Strings

A String basically is an array of characters. Strings are used in almost any programming language. The Java programming language provides a String wrapper-class which is useful for creating and manipulating strings.

Creating Strings

The easiest way to create a String is to use the following syntax:

String hello = "Hello World!";

"Hello World!" is a string literal that is assigned to the variable quote. This line of code is equivalent to creating a character array and calling the string constructor with it:

char[] hello = { 'H', 'e', 'l', 'l', 'o', ' ', 'W' , 'o', 'r', 'l', 'd', '!'};

String helloString = new String(hello);

In fact, every time the compiler reads a string literal enclosed in quotes in your code, it will automatically create a String object for that character sequence.

String concatenated = "ABC" + "DEF"; //"ABCDEF"

That means that the statement above will result in three String objects being created. The two Strings that are used to create the concatenated one will be discarded immediately though.

Please keep in mind: Although the String class is a wrapper for the underlying character array, it can not be accessed directly by any means. Strings are immutable. That means you cannot directly change a String object. hello and concatenated in this case are only references to String objects somewhere in the memory. Thus, you can change them like any other variable. But each time you modify the content of a String reference, a new String with the desired content is created. This can cause some trouble when you want to modify a String very often, which might be the case if you want to concatenate many Strings together. In such cases, you may consider using a StringBuilder for increased performance

What you can do with Strings

As you might already know, you can print a String to the console using the standard print methods like println.

String hello = "Hello World!";

Often times it is important to know how many characters the string contains - or in other words - to determine the length of the string. This can be achieved by the length method.

String hello = "Hello World!";
int length = hello.length(); //Assign 12 to length

Since our String variables are only references, they cannot be compared with the logical operators such as == or !=. Instead, you may use predefined functions from the String class.

String s1 = "Hello";
String s2 = "HELLO";

s1.equals(s2); // false
s1.equalsIgnoreCase(s2); // true
s1.compareTo(s2); // > 0 because 'e' comes after 'E'

The function equals compares the two Strings and returns a boolean value whether the Strings are equal or not. equalsIgnoreCase can be used if the capitalization of the two Strings does not matter. compareTo can not only be used to check, if two Strings are the same but also to sort them lexicographically. The function will return 0 if both Strings are equal, a negative integer if the first string is "smaller" and a positive integer if the String is "greater".

Modifying Strings

Strings have some predefined methods for modifying them. Keep in mind though that each time these functions are called, a new String is being created that has to be assigned to a variable.

String s1 = "Hello";
String s2;
s2 = s1.toLowerCase(); // "hello"
s2 = s1.String toUpperCase(); // "HELLO"
s2 = s1.String trim(); // "Hello" (removes trailing and leading whitespaces)
s2 = s1.String replace('e', 'a'); // "Hallo"
char[] charArray = s1.toCharArray(); // create a char array from s1

If you choose to create a char array from the String, you can perform any changes on it and put it in a new String object. This is the simplest method of doing a custom modification to a String.