Strings are simply sequences of characters. In Java, strings are objects. The String class is used to create and manipulate strings. The easiest way to create a string is to write:

String helloString = "Hello world!";

“Hello world!” is a string literal, a sequence of characters enclosed in double quotes. Whenever the compiler encounters a string literal it creates a String object. You can also create a string literal using a keyword and a constructor. The String class has thirteen constructors. These allow you to specify the initial value of the string in different ways, for example:

char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.' };
String helloString = new String(helloArray);

The last line of this code fragment displays “hello” on the screen.

You can use the length() method, to determine the number of characters in a string object. The following code fragment would set the value of len to 26.

String palindrome = "Able was I, ere I saw Elba";
int len = palindrome.length();

You can use the concat() method to concatenate two strings (join them together):

string3 = string1.concat(string2);

String 3 will be string1 with string2 added to the end.

You can also use the concat() method with string literals:

"My name is ".concat("Dumbledore");

You can also concatenate strings using the + operator:

"Hello," + " world" + "!"

would produce the string “Hello, world!”

Next: Record