The String
methods used on the AP CS A Exam are listed at the top of the AP CS A Java Quick Reference.
String(String)
constructor and equals
method
String
objects are commonly constructing by enclosing a sequence of characters in double quotes. String
objects can also be constructed by explicitly calling the String
constructor that takes a String
. The behavior is subtly different because of the Java String Pool. The String
constructor that takes a String
parameter explicitly constructs a new String
object, even if an identical object already exists in the Pool.
The equals
method returns true
if the implicit and explict parameters represent the same sequence of characters; otherwise, it returns false
. All comparisons of String
objects for equality should use the equals
method (or the compareTo
method).
The Java String Pool makes it possible to write examples in which the ==
operator and the equals
method appear to behave identically. It is not necessary to recognize such examples on the AP CS A Exam. The ==
operator should not be used to compare String
objects for equality, even though it appears to work in some examples.
The equals
method accepts null
as an explicit parameter/argument, and returns false
. As with all method calls, null
can not be an implicit parameter.
String(String)
constructor and equals
method examples
String str1 = "Brandon";
String str2 = "Brandon";
String str3 = new String("Brandon");
String str4 = "Horn";
System.out.println(str1.equals(str2)); // prints true
System.out.println(str1.equals(str3)); // prints true
System.out.println(str1.equals(str4)); // prints false
System.out.println(str1.equals(null)); // prints false
// The examples below do NOT show the correct way to compare
// String objects, even though some output the correct result.
System.out.println(str1 == str2); // prints true
System.out.println(str1 == str3); // prints false
System.out.println(str1 == str4); // prints false
System.out.println(null.equals(str1)); // compile time error
length
method
The length
method returns the number of characters in a String
. It does not return the index of the last character.
length
examples
String name = "Brandon";
// 0123456
String empty = "";
System.out.println(name.length()); // prints 7
System.out.println(empty.length()); // prints 0
substring(int, int)
method
The 2 parameter substring
method returns a new String
containing the characters starting at the first index up to but not including the second index. Since the character at the second index is not included, the second index can be 1 larger than the last valid index.
If the 2 parameter substring
method is called with the same index value for both parameters, the behavior depends on the index. If the index if valid or no more than 1 larger than the last valid index, the method returns an empty String
. If the index is invalid, other than as described, the method throws an exception.
String
objects are immutable. A call to substring
returns a new String
. It does not modify the implicit parameter. All methods of String
that appear to modify the implicit parameter actually return a new String
.
substring(int, int)
examples
String name = "Brandon";
// 0123456
String empty = "";
System.out.println(name.substring(2, 5)); // prints "and"
System.out.println(name.substring(2, 3)); // prints "a"
System.out.println(name.substring(4, 4)); // prints ""
System.out.println(name.substring(6, 7)); // prints "n"
System.out.println(name.substring(7, 7)); // prints ""
System.out.println(name.substring(0, name.length())); // prints "Brandon"
System.out.println(empty.substring(0, 0)); // prints ""
// Each of the statements below throws a StringIndexOutOfBoundsException.
System.out.println(name.substring(6, 8));
System.out.println(name.substring(7, 8));
System.out.println(name.substring(8, 8));
System.out.println(name.substring(-1, 0));
System.out.println(name.substring(-1, -1));
System.out.println(empty.substring(0, 1));
substring(int)
method
The 1 parameter substring
method returns a new String
containing the character(s) starting at the parameter index to the end of the String
.
The 1 parameter substring
method allows the index to be 1 larger than the last valid index in the String
, without throwing an exception. If the parameter index is the length, the 1 parameter substring
method returns an empty String
.
As with substring(int, int)
, substring(int)
does not modifiy the implicit parameter. It returns a new String.
substring(int)
examples
String name = "Brandon";
// 0123456
String empty = "";
System.out.println(name.substring(5)); // prints "on"
System.out.println(name.substring(6)); // prints "n"
System.out.println(name.substring(7)); // prints ""
System.out.println(name.substring(0)); // prints "Brandon"
System.out.println(empty.substring(0)); // prints ""
// Each of the statements below throws a StringIndexOutOfBoundsException.
System.out.println(name.substring(8));
System.out.println(empty.substring(1));
indexOf
method
The indexOf
method finds where one String
(the explicit parameter/argument) starts inside another String
(the implicit parameter). indexOf
returns -1
if the explicit parameter is not found in the implicit parameter.
The String
method contains
is not used on the AP CS A multiple choice. The indexOf
method is used to check if a String
contains another String
.
indexOf
examples
String name = "Brandon";
// 0123456
String empty = "";
System.out.println(name.indexOf("a")); // prints 2
System.out.println(name.indexOf("n")); // prints 3
System.out.println(name.indexOf("and")); // prints 2
System.out.println(name.indexOf("j")); // prints -1
System.out.println(name.indexOf("")); // prints 0
System.out.println(empty.indexOf("")); // prints 0
System.out.println(empty.indexOf("j")); // prints -1
System.out.println(name.indexOf("and") != -1); // prints true
System.out.println(name.indexOf("n") >= 0); // prints true
System.out.println(name.indexOf("or") != -1); // prints false
compareTo
method
The compareTo
method is used to compare String
objects for order. See compareTo on the AP CS A Exam for a detailed explanation and examples.
String
concatenation
The +
operator performs concatenation when at least one of its operands is a String
.
The +=
operator performs concatenation when the left operand is a String
.
The char
data type is not used on the AP CS A Exam. When both operands to the +
operator are of type char
, the operator adds the unicode values. Use String
objects and substring
calls instead of char
values and charAt
calls to avoid this issue (and others) on the AP CS A free response.
String
concatenation examples
"1" + "2"
evaluates to "12"
.
"1" + 2 + 3
evaluates to "123"
.
The order operations is ("1" + 2) + 3
. The result of "1" + 2
is a String
.
1 + "2" + 3
evaluates to "123"
.
The order operations is (1 + "2") + 3
. The result of 1 + "2"
is a String
.
1 + 2 + "3"
evaluates to "33"
.
The order of operations is (1 + 2) + "3"
. The result of 1 + 2
is an int
.
1 + (2 + "3")
evaluates to "123"
.
String str = "";
str += 2;
str += " fish";
System.out.println(str); // prints "2 fish"
Practice String
manipulation with AP CS Tutor Brandon Horn.
String
immutability
String name = "Brandon";
name.toUpperCase();
System.out.println(name); // prints Brandon
name = name.toUpperCase();
System.out.println(name); // prints BRANDON
String
objects are immutable. Once a String
object has been created, it cannot be changed. All of the methods of String
that appear to change the String
actually return a new String
.
As demonstrated above, it is possible to assign an existing variable to refer to the new String
.
It is common for beginning programmers to work with String
objects before understanding much about the difference between primitive types and objects. Primitive types vs references exercises demonstrates the difference.
char
values
The char
data type is not part of the AP Java Subset and is not used on the AP CS A Exam. I discourage students from using the char
type in AP CS free response answers since it can lead to common errors (in particular, adding the Unicode values of 2 char
values).
The pages below contain information about the char
data type.
Help & comments
Get help from AP CS Tutor Brandon Horn