Skip to content

The String Class

Estimated time to read: 1 minute

Overview

The String class is one of the most commonly used classes in Java

A String objects represents an ordered sequence of characters, not that these are Unicode not ASCII.

Documentation

Documentation - The Oracle Java documentation covers the attributes and methods that can be called from many of the java.lang classes, and more. This is a good read and is highly detailed.

String Documentation

The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class. Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared. For example:

String str = "abc";

is equivalent to:

char data[] = {'a', 'b', 'c'};
String str = new String(data);

Here are some more examples of how strings can be used:

System.out.println("abc"); // String literals in double quotes
String cde = "cde"; 
System.out.println("abc" + cde); // String concatenation 
String c = "abc".substring(2, 3); // Substring. Using a String literal as an object of a method
String d = cde.substring(1, 2);

Notes

  • String literals can never be null
  • Strings are immutable.
  • When manipulating a string, the method must return a new string.