OCA Java SE 8 Mock Exam Review - Java String Review

String class represents an immutable sequence of characters.

A String object can be created

  • by using the operator new,
  • by using the assignment operator (=), or
  • by using double quotes.

String objects created using the assignment operator are stored in a pool of String objects.

String objects in the string value pool is reused.

String objects created using the operator new are never placed in the pool of String objects.

The comparison operator (==) compares String references.

equals method compares the String values.

None of the methods defined in the class String can modify its value, string is immutable.

The method charAt(int index) retrieves a character at a specified index of a String.

The method indexOf searches a String for the occurrence of a char or a String, starting from the first position or a specified position.

The method substring returns a portion of a String object.

The method substring method doesn't include the character at the end position.

The trim method will return a new String by removing all the leading and trailing white spaces in a String.

The trim method doesn't remove any white space within a String.

The method length returns the length of a String.

The method startsWith determines whether a String starts with a String.

The method endsWith determines whether a String ends with a String.

When chained, the methods are evaluated from left to right.

Java supports concatenating String objects by using the operators + and +=.

To compare two String values for equality use the method equals defined in the String class.

The comparison operator == determines whether both the reference variables are referring to the same String objects.


The StringBuilder defined in package java.lang represents a mutable sequence of characters.

The StringBuilder class is used to modify a sequence of characters.

The StringBuilder class is mutable, the value of a StringBuilder object can be modified without creating a new StringBuilder object.

A StringBuilder object can be created using its constructors, which can accept either a String object, another StringBuilder object, an int value to specify the capacity of StringBuilder, or nothing.

The append method adds the specified value at the end of the existing value of a StringBuilder object.

The insert method inserts characters at a specified position in a StringBuilder object.

The insert method inserts the data at a particular position.

Append method adds the requested data at the end of the StringBuilder object.

The method delete removes the characters in a substring of the specified StringBuilder.

The method deleteCharAt removes the char at the specified position.

The method reverse reverses the sequence of characters of a StringBuilder.

The replace method replaces a sequence of characters with another String.