Many times we need a data type to represent words and phrases. In Java (and many other languages) we call this type String.
Strings are somewhat special in Java. Although they are not primitives, like ints or booleans, they have a constant representation. Also, like primitives, String values are immutable (you cannot change their value, although you can use them to create other values).
In Java, you declare String variables by using the data type String (notice the capital S) as such:
String s1;We denote constants of type string by writing the word inside double quotes, "like this".
String word1="one";
String word2="word";Strings are composed of characters (letters, digits and such); the primitive data type that can contains characters is char, and we can declare constants of this type by using single quote marks, like 'a'.
char letter='b';Sometimes we need to use weird characters (like the tab character, or the double quotation mark) inside a String; in Java we use a slash, \ to escape the weird characters; for example, \t is the tab character (and we'd use " if we need to include " in a String constant).
Strings have many methods; some of the most important ones are:
- .length() - returns the length of the string
- .equals(anotherString) - compares two strings for equality (== compares for identity, might work the same)
- .compareTo and .compareToIgnoreCase, return -1, 0 or 1 after comparing the Strings
- concat - allows you to concatenate two strings; can also use +
- substring - allows you to select a piece of the string (another string)
- charAt - returns the character at a given space (indexing starts at 0)