LESSON 8
3.1 Lesson Outcomes
After completing this lesson, learners will be able to:
- Define strings in Java.
- Create and initialize string variables.
- Use common string methods.
- Concatenate and compare strings.
- Explain the importance of strings in Java applications.
3.2 Overview
Strings are one of the most commonly used data types in Java programming. Strings are used to store and process text such as names, messages, addresses, passwords, and user input.
This lesson introduces learners to Java strings and explains how strings are created and manipulated within Java applications. Learners will also explore commonly used string methods and operations used in real-world software systems.
Strings are widely used in:
- banking systems,
- websites,
- mobile applications,
- inventory systems,
- and enterprise software.
Understanding strings is essential because most software applications process textual data.
KT0801 — Introduction to Strings
A string is a sequence of characters stored as an object in Java.
Strings use the String class.
Example of a String
String name = "Java";
Explanation
| Component | Description |
|---|---|
| String | Reference data type |
| name | Variable name |
| “Java” | String value |
Importance of Strings
Strings are used to:
- store text,
- display messages,
- process user input,
- and manage application data.
KT0802 — Creating Strings
Strings can be created using:
- string literals,
- or the new keyword.
String Literal Example
String language = "Java";
Using the new Keyword
String language = new String("Java");
Difference Between the Two Approaches
| String Literal | new Keyword |
|---|---|
| Simpler syntax | Creates new object |
| Memory efficient | Allocates new memory |
| Commonly used | Less commonly used |
KT0803 — String Concatenation
Concatenation combines strings together.
Java uses the + operator for concatenation.
Example
public class Main {
public static void main(String[] args) {
String firstName = "Bukhosi";
String lastName = "Moyo";
String fullName = firstName + " " + lastName;
System.out.println(fullName);
}
}
Output
Bukhosi Moyo
Importance of Concatenation
Concatenation is used to:
- build messages,
- combine text,
- and generate application output.
KT0804 — Common String Methods
The String class provides built-in methods for processing text.
length() Method
Returns the number of characters in a string.
String word = "Java";
System.out.println(word.length());
Output
4
toUpperCase() Method
Converts text to uppercase.
String word = "java";
System.out.println(word.toUpperCase());
Output
JAVA
toLowerCase() Method
Converts text to lowercase.
String word = "JAVA";
System.out.println(word.toLowerCase());
Output
java
charAt() Method
Returns a character at a specified index.
String word = "Java";
System.out.println(word.charAt(0));
Output
J
KT0805 — Comparing Strings
Strings can be compared using the equals() method.
Example
public class Main {
public static void main(String[] args) {
String word1 = "Java";
String word2 = "Java";
System.out.println(word1.equals(word2));
}
}
Output
true
Importance of equals()
The equals() method checks:
- user input,
- passwords,
- usernames,
- and application conditions.
KT0806 — String Immutability
Strings in Java are immutable.
This means:
- string values cannot be changed after creation.
When modifications occur:
- Java creates a new string object.
Example
String text = "Java";
text = text + " Programming";
System.out.println(text);
Output
Java Programming
Importance of Immutability
Immutability improves:
- security,
- performance,
- and memory management.
KT0807 — Real-World Use of Strings
Strings are used in:
- login systems,
- registration forms,
- banking systems,
- search engines,
- and communication systems.
Examples:
- customer names,
- email addresses,
- passwords,
- product descriptions,
- and transaction details.
Strings are essential in nearly every software application.
3.5 Key Notes / Summary
- Strings store text in Java.
- Strings use the String class.
- Strings are reference data types.
- The + operator concatenates strings.
- String methods simplify text processing.
- equals() compares string values.
- Strings are immutable in Java.