Java ProgramsStringsConvert String to Lowercase

Convert String to Lowercase in Java

beginner·  Strings  ·  String Manipulation

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.

Input
Hello Java
Output
hello java

Java Program

Java
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

hello java

Core Logic

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

How It Works
  1. 1A for-each loop visits each character of the string in turn.
  2. 2Character.toLowerCase(c) converts the current character to its lowercase form.
  3. 3For a character that's already lowercase, a digit, or punctuation, toLowerCase() 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' 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.

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.toLowerCase()StringBuilderfor-each loop

Approach 2: Using toLowerCase()

Java
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

hello java

Core Logic

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

How It Works
  1. 1str.toLowerCase() 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".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.

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

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

String.toLowerCase()

Approach 3: Java 8

Java
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

hello java

Core Logic

The same per-character conversion can be expressed as a stream — map each character to its lowercase 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.toLowerCase((char) c))) maps each code to its lowercase 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 toLowerCase() 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 lowercase form and Collectors.joining() rebuilds a result string holding all n characters.

Key Concepts

Streamchars()Collectors.joining()

Related Programs