Strings

A 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 variable
String name = "Codecademy";
// Creating another String variable
String address = "575 Broadway #5, New York, NY 10012";

To compare Strings, the .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-sensitive
System.out.println(name.equals("bob"));

Strings

.charAt()
Returns the character at the given index in the string.
.codePointAt()
Returns the Unicode value at the given index in the string.
.codePointBefore()
Returns the Unicode value before the given index in the string.
.codePointCount()
Returns the number of Unicode values in specified range of a string.
.compareTo()
Compares two strings lexicographically based on the Unicode value of each character in the string.
.compareToIgnoreCase()
Compares two strings lexicographically based on the Unicode value of each character in the string while ignoring lower case and upper case differences.
.concat()
Returns a string that is the concatenation of the given strings.
.contains()
Returns true if a sequence of characters exists in a given string, otherwise false.
.contentEquals()
Returns true if the sequence of characters in the string is equal to the content of the specified string. If not, returns false.
.copyValueOf()
Returns a string with characters copied from an array.
Edit this page on GitHub

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your feedback.

Learn Java on Codecademy

Edit this page on GitHub

Contributors