String in Java is an object that holds a sequence of characters contained within a pair of double quotes (
"). It is not a primitive datatype.
Java strings provide a way to store something like a word, sentence, or whole paragraph. They can be any length and can contain letters, numbers, symbols, and spaces.
// Creating a String variableString name = "Codecademy";// Creating another String variableString address = "575 Broadway #5, New York, NY 10012";
.equals() method must be used instead of the primitive equality comparator
.equals() will compare the values of the strings, while
== compares the references (location in memory) of the strings.
String name = "Bob";// The following will print "false" because strings are case-sensitiveSystem.out.println(name.equals("bob"));
- Returns the character at the given index in the string.
- Returns the Unicode value at the given index in the string.
- Returns the Unicode value before the given index in the string.
- Returns the number of Unicode values in specified range of a string.
- Compares two strings lexicographically based on the Unicode value of each character in the string.
- Compares two strings lexicographically based on the Unicode value of each character in the string while ignoring lower case and upper case differences.
- Returns a string that is the concatenation of the given strings.
- Returns true if a sequence of characters exists in a given string, otherwise false.
- Returns true if the sequence of characters in the string is equal to the content of the specified string. If not, returns false.
- Returns a string with characters copied from an array.
- Anonymous contributors
Learn Java on Codecademy
- Anonymous contributorsAnonymous contributors