Convert String to Lowercase in Java
Problem
Converting a string to lowercase means transforming every uppercase letter into its lowercase form, leaving non-letters unchanged.
Given a string, convert it to lowercase.
Java Program
public class ConvertToLowercaseManual {
public static void main(String[] args) {
String str = "Hello Java";
StringBuilder result = new StringBuilder();
for (char c : str.toCharArray()) {
result.append(Character.toLowerCase(c)); // non-letters pass through unchanged
}
System.out.println(result.toString());
}
}Output
Core Logic
Converting each character individually and appending it to a result buffer builds the lowercase string one character at a time.
- 1A for-each loop visits each character of the string in turn.
- 2
Character.toLowerCase(c)converts the current character to its lowercase form. - 3For a character that's already lowercase, a digit, or punctuation,
toLowerCase()just returns it unchanged. - 4Each converted character is appended to a
StringBuilder, building the result one character at a time.
"Hello Java", 'H' becomes 'h', 'J' becomes 'j', and every already-lowercase letter stays as it is, producing "hello java".Key Point: The space between the two words passes through toLowerCase() unchanged — it only ever affects actual letters.
Why: Each character is visited once and appended to a result buffer that grows to match the input length.
Key Concepts
Approach 2: Using toLowerCase()
public class ConvertToLowercaseBuiltin {
public static void main(String[] args) {
String str = "Hello Java";
// toLowerCase() already handles the per-character conversion internally
System.out.println(str.toLowerCase());
}
}
Output
Core Logic
In real code, there's no reason to loop manually — toLowerCase() already converts the whole string in one call.
- 1
str.toLowerCase()takes the original string and returns a brand-new string with every letter converted. - 2Internally, it performs the same kind of per-character conversion as the manual loop.
- 3No explicit loop or
StringBuilderis needed in your own code.
"Hello Java".toLowerCase() returns "hello java" in a single call.Key Point: This is the version to actually use — the manual loop exists only to show what toLowerCase() is conceptually doing under the hood.
Why: toLowerCase() still has to build a new string with every character converted, but that scan happens inside the JDK instead of your own loop.
Key Concepts
Approach 3: Java 8
import java.util.stream.Collectors;
public class ConvertToLowercaseStream {
public static void main(String[] args) {
String str = "Hello Java";
// Maps each character to its lowercase form, then joins them back into a String
String result = str.chars()
.mapToObj(c -> String.valueOf(Character.toLowerCase((char) c)))
.collect(Collectors.joining());
System.out.println(result);
}
}
Output
Core Logic
The same per-character conversion can be expressed as a stream — map each character to its lowercase form and join the results.
- 1
str.chars()returns anIntStreamof the string's character codes. - 2
.mapToObj(c -> String.valueOf(Character.toLowerCase((char) c)))maps each code to its lowercase single-characterString. - 3
.collect(Collectors.joining())concatenates all the converted characters back into one result string.
"Hello Java" character by character converts each letter, and Collectors.joining() reassembles "hello java".Key Point: This does the same work as toLowerCase() in a far more roundabout way — it exists mainly to show the character-by-character conversion as an explicit stream pipeline.
Why: Each character is mapped to its lowercase form and Collectors.joining() rebuilds a result string holding all n characters.