Java ProgramsStringsConvert String to Uppercase

Convert String to Uppercase in Java

beginner·  Strings  ·  String Manipulation

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.

Input
Hello Java
Output
HELLO JAVA

Java Program

Java
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

HELLO JAVA

Core Logic

Converting each character individually and appending it to a result buffer builds the uppercase string one character at a time.

How It Works
  1. 1A for-each loop visits each character of the string in turn.
  2. 2Character.toUpperCase(c) converts the current character to its uppercase form.
  3. 3For a character that's already uppercase, a digit, or punctuation, toUpperCase() just returns it unchanged.
  4. 4Each converted character is appended to a StringBuilder, building the result one character at a time.
For "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.

Complexity
Time Complexity: O(n)Space Complexity: O(n)

Why: Each character is visited once and appended to a result buffer that grows to match the input length.

Key Concepts

Character.toUpperCase()StringBuilderfor-each loop

Approach 2: Using toUpperCase()

Java
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

HELLO JAVA

Core Logic

In real code, there's no reason to loop manually — toUpperCase() already converts the whole string in one call.

How It Works
  1. 1str.toUpperCase() takes the original string and returns a brand-new string with every letter converted.
  2. 2Internally, it performs the same kind of per-character conversion as the manual loop.
  3. 3No explicit loop or StringBuilder is 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.

Complexity
Time Complexity: O(n)Space Complexity: O(n)

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

String.toUpperCase()

Approach 3: Java 8

Java
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

HELLO JAVA

Core Logic

The same per-character conversion can be expressed as a stream — map each character to its uppercase form and join the results.

How It Works
  1. 1str.chars() returns an IntStream of the string's character codes.
  2. 2.mapToObj(c -> String.valueOf(Character.toUpperCase((char) c))) maps each code to its uppercase single-character String.
  3. 3.collect(Collectors.joining()) concatenates all the converted characters back into one result string.
Mapping "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.

Complexity
Time Complexity: O(n)Space Complexity: O(n)

Why: Each character is mapped to its uppercase form and Collectors.joining() rebuilds a result string holding all n characters.

Key Concepts

Streamchars()Collectors.joining()

Related Programs