Convert String to Uppercase in Java
Problem
Converting a string to uppercase means transforming every lowercase letter into its uppercase form, leaving non-letters unchanged.
Given a string, convert it to uppercase.
Java Program
public class ConvertToUppercaseManual {
public static void main(String[] args) {
String str = "Hello Java";
StringBuilder result = new StringBuilder();
for (char c : str.toCharArray()) {
result.append(Character.toUpperCase(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 uppercase string one character at a time.
- 1A for-each loop visits each character of the string in turn.
- 2
Character.toUpperCase(c)converts the current character to its uppercase form. - 3For a character that's already uppercase, a digit, or punctuation,
toUpperCase()just returns it unchanged. - 4Each converted character is appended to a
StringBuilder, building the result one character at a time.
"Hello Java", 'H' stays 'H', 'e' becomes 'E', and so on, producing "HELLO JAVA".Key Point: The space between the two words passes through toUpperCase() 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 toUpperCase()
public class ConvertToUppercaseBuiltin {
public static void main(String[] args) {
String str = "Hello Java";
// toUpperCase() already handles the per-character conversion internally
System.out.println(str.toUpperCase());
}
}
Output
Core Logic
In real code, there's no reason to loop manually — toUpperCase() already converts the whole string in one call.
- 1
str.toUpperCase()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".toUpperCase() returns "HELLO JAVA" in a single call.Key Point: This is the version to actually use — the manual loop exists only to show what toUpperCase() is conceptually doing under the hood.
Why: toUpperCase() 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 ConvertToUppercaseStream {
public static void main(String[] args) {
String str = "Hello Java";
// Maps each character to its uppercase form, then joins them back into a String
String result = str.chars()
.mapToObj(c -> String.valueOf(Character.toUpperCase((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 uppercase form and join the results.
- 1
str.chars()returns anIntStreamof the string's character codes. - 2
.mapToObj(c -> String.valueOf(Character.toUpperCase((char) c)))maps each code to its uppercase 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 toUpperCase() 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 uppercase form and Collectors.joining() rebuilds a result string holding all n characters.