chapter five

5 String APIs

 

Exam objectives covered in this chapter

What you need to know

[3.4] Creating and manipulating Strings

How to initialize String variables using String literals and new operator.

Use of the operators =, +=, !=, and == with String objects. Pooling of string literal values. Literal value for class String.

Usage of methods from class String. Immutable String values. All the String methods manipulate and return a new String object.

[3.5] Manipulate data using the StringBuilder class and its methods

How to create StringBuilder classes and how to use their commonly used methods.

Difference between StringBuilder and String classes. Difference between methods with similar names defined in both classes.

[3.4] Creating and manipulating Strings

How to initialize String variables using String literals and new operator.

Use of the operators =, +=, !=, and == with String objects. Pooling of string literal values. Literal value for class String.

Usage of methods from class String. Immutable String values. All the String methods manipulate and return a new String object.

In the OCP Java SE 11 Programmer I exam, you’ll be asked many questions about how to create, modify, and delete String and StringBuilder objects. To prepare you for such questions, in this chapter I’ll provide insight into the variables you’ll use to store these objects’ values, along with definitions for some of their methods.

In this chapter, we’ll cover the following:

5.1   Welcome to the world of the String class

5.1.1   Compact Strings

5.1.2   Creating String objects

5.1.3   The class String is immutable

5.1.4   Methods of the class String

5.1.5   String objects and operators

5.1.6   Determining equality of Strings

5.2   Mutable strings: StringBuilder

5.2.1   The StringBuilder class is mutable

5.2.2   Creating StringBuilder objects

5.2.3   Methods of class StringBuilder

5.2.4   A quick note on the class StringBuffer

5.3   Summary

5.4   Sample exam questions

5.5   Answers to sample exam questions