3.1 Lesson Outcomes
After completing this lesson, learners will be able to:
- Create and use Strings in Java.
- Concatenate Strings.
- Compare String values.
- Apply String methods.
- Manipulate and format text in Java programs.
3.2 Overview
Strings are reference types used to store and manipulate text in Java applications. Java provides a wide range of String methods used for searching, formatting, comparing, and modifying text.
This lesson introduces learners to practical String operations used in software development.
Strings are important in:
- user input,
- data processing,
- software interfaces,
- and application communication.
Understanding Strings is important because text processing is used in almost every Java application.
PA0801 — Create a String
Strings store text values in Java.
Example String Declaration
Java
String name = “Alice”;
Creating Strings Using new Keyword
Java
String city = new String(“Johannesburg”);
Practical Activity
Learners must:
- create String variables,
- assign text values,
- and display String output.
PA0802 — Concatenate Strings
String concatenation combines multiple text values.
Example Concatenation
Java
String firstName = “John”;
String lastName = “Smith”;
String fullName = firstName + ” ” + lastName;
Example Output
Plain text
John Smith
Practical Activity
Learners must:
- concatenate Strings,
- combine text values,
- and display formatted results.
PA0803 — Use String Methods
Java provides built-in methods for String manipulation.
Common String Methods
|
Method |
Purpose |
|---|---|
|
length() |
Returns String length |
|
toUpperCase() |
Converts text to uppercase |
|
toLowerCase() |
Converts text to lowercase |
|
charAt() |
Returns character at index |
|
substring() |
Extracts part of a String |
Example
Java
String word = “Java”;
System.out.println(word.length());
Practical Activity
Learners must:
- apply String methods,
- manipulate text,
- and display method results.
PA0804 — Compare Strings
Strings can be compared using:
Plain text
equals()
method.
Example
Java
String a = “Java”;
String b = “Java”;
System.out.println(a.equals(b));
Practical Activity
Learners must:
- compare String values,
- test equality,
- and display results.
PA0805 — Search Within Strings
Strings can be searched using built-in methods.
Example contains()
Java
String text = “Welcome to Java”;
System.out.println(text.contains(“Java”));
Example indexOf()
Java
System.out.println(text.indexOf(“Java”));
Practical Activity
Learners must:
- search text,
- identify word positions,
- and display search results.
PA0806 — Remove Spaces and Format Strings
Java provides methods for text formatting.
Example trim()
Java
String text = ” Java “;
System.out.println(text.trim());
Example replace()
String text = “Java”;
System.out.println(text.replace(“Java”, “Python”));
Practical Activity
Learners must:
- remove spaces,
- replace words,
- and format String output.
PA0807 — Convert Between Strings and Numbers
Java allows conversion between Strings and numeric values.
String to Integer
Java
String num = “100”;
int value = Integer.parseInt(num);
Integer to String
Java
int age = 25;
String text = String.valueOf(age);
Practical Activity
Learners must:
- convert Strings to numbers,
- convert numbers to Strings,
- and display results.
PA0808 — Create Complete String Program
Strings are commonly used in software applications.
Example Program
Java
public class StringExample {
public static void main(String[] args) {
String firstName = “John”;
String lastName = “Smith”;
String fullName =
firstName + ” ” + lastName;
System.out.println(fullName);
System.out.println(fullName.toUpperCase());
System.out.println(fullName.length());
}
}
Importance of Strings
Strings support:
- text processing,
- user interaction,
- and application communication.
3.5 Key Notes / Summary
- Strings store text in Java.
- Strings are reference types.
- Strings can be concatenated using
+. - Java provides many String methods.
- equals() compares String values.
- Strings can be searched and formatted.
- Strings are widely used in software applications