Sub topic 9
Strings and String Manipulation
A string is a piece of text, like "Hello". In Java, strings are objects, not simple values. This chapter shows how to create strings, join them, and use the most useful built-in string methods.
Creating a String
You create a string by putting text in double quotes:
String greeting = "Hello, world!";
System.out.println(greeting);String is the type, and greeting is the variable name. Strings are not primitive types (like int or double). They are objects, which means they come with built-in methods you can call.
Joining Strings
The + operator joins strings together. This is called concatenation:
String first = "Ada";
String last = "Lovelace";
String fullName = first + " " + last;
System.out.println(fullName); // Ada LovelaceYou can also join strings with numbers:
int age = 36;
System.out.println("Age: " + age); // Age: 36When + is used with a string, Java converts the other value to text automatically.
Useful String Methods
A method is a built-in action a string can perform. Here are the most common ones:
| Method | What it does | Example result |
|---|---|---|
| `length()` | Counts characters | `"Java".length()` is `4` |
| `charAt(0)` | Gets one character | `"Java".charAt(0)` is `'J'` |
| `toUpperCase()` | Makes all letters capital | `"java".toUpperCase()` is `"JAVA"` |
| `toLowerCase()` | Makes all letters small | `"JAVA".toLowerCase()` is `"java"` |
| `contains("av")` | Checks if text appears inside | `"Java".contains("av")` is `true` |
| `substring(1, 3)` | Copies part of the string | `"Java".substring(1, 3)` is `"av"` |
| `equals("Java")` | Compares two strings | `"Java".equals("Java")` is `true` |
| `trim()` | Removes spaces at both ends | `" hi ".trim()` is `"hi"` |
Notice that counting starts at 0, not 1. The first character of "Java" is at position 0.
Here is a full program using several methods:
public class StringDemo {
public static void main(String[] args) {
String word = " Java ";
String clean = word.trim();
System.out.println("Length: " + clean.length());
System.out.println("First letter: " + clean.charAt(0));
System.out.println("Uppercase: " + clean.toUpperCase());
System.out.println("Has 'av'? " + clean.contains("av"));
System.out.println("Middle part: " + clean.substring(1, 3));
}
}Strings Are Immutable
Strings in Java are immutable, which means they cannot be changed after they are created. Methods like toUpperCase() do not change the original string. They return a new string instead:
String name = "ada";
name.toUpperCase(); // The original is still "ada"
String upper = name.toUpperCase(); // You must save the result
System.out.println(upper); // ADAForgetting to save the result is a common beginner mistake. If nothing uses the returned value, it is simply lost.
Comparing Strings: Use equals, Not ==
To check whether two strings have the same text, use .equals(). Do not use == for this. The == operator checks whether two variables point to the same object in memory, not whether the text matches:
String a = new String("Java");
String b = new String("Java");
System.out.println(a == b); // false (different objects)
System.out.println(a.equals(b)); // true (same text)callout tip When comparing string text, always write s1.equals(s2), never s1 == s2. This avoids confusing bugs.
Try It Yourself
Type the StringDemo program into a file called StringDemo.java, compile it with javac StringDemo.java, and run it with java StringDemo. Then change the string and see which outputs change.
Exercise
What does "banana".substring(2, 5) return?
Show answer
"nan". The substring starts at index 2 ('n') and stops before index 5, so it takes characters at positions 2, 3, and 4.
Exercise
Why does System.out.println("ada".toUpperCase()); print ADA, but the string "ada" itself is unchanged?
Show answer
Strings are immutable. toUpperCase() builds and returns a new string; the original "ada" stays the same. Here the new string is printed directly, so nothing is lost.