YouTip LogoYouTip

Character Tolowercase

## Java Character.toLowerCase() Method The `Character.toLowerCase(char ch)` method is a built-in static method in Java's `Character` class. It is used to convert a specified uppercase character to its lowercase equivalent. If the character passed to the method is already in lowercase, or if it is a non-alphabetic character (such as a digit or punctuation mark) that does not have a lowercase counterpart, the method returns the character itself unchanged. --- ## Syntax ```java public static char toLowerCase(char ch) ``` ### Parameters * **`ch`**: The primitive `char` value that you want to convert to lowercase. ### Return Value * Returns the lowercase equivalent of the character `ch`, if it exists. * Otherwise, it returns the original character `ch` itself. --- ## Code Examples ### 1. Basic Usage The following example demonstrates how to use `Character.toLowerCase()` to convert uppercase letters, lowercase letters, and non-alphabetic characters. ```java public class LowerCaseExample { public static void main(String[] args) { // Convert an uppercase letter System.out.println(Character.toLowerCase('A')); // Output: a // Convert a lowercase letter (remains unchanged) System.out.println(Character.toLowerCase('g')); // Output: g // Convert a number (remains unchanged) System.out.println(Character.toLowerCase('9')); // Output: 9 // Convert a special character (remains unchanged) System.out.println(Character.toLowerCase('$')); // Output: $ } } ``` **Output:** ```text a g 9 $ ``` ### 2. Converting a String to Lowercase (Character by Character) If you want to convert an entire string to lowercase character by character (for example, when processing a stream of characters), you can loop through the string as shown below: ```java public class StringConversionExample { public static void main(String[] args) { String original = "Hello, WORLD 123!"; StringBuilder result = new StringBuilder(); for (int i = 0; i < original.length(); i++) { char currentChar = original.charAt(i); // Convert each character and append to the result result.append(Character.toLowerCase(currentChar)); } System.out.println("Original: " + original); System.out.println("Lowercase: " + result.toString()); } } ``` **Output:** ```text Original: Hello, WORLD 123! Lowercase: hello, world 123! ``` --- ## Key Considerations 1. **Unicode Support**: The `Character.toLowerCase(char ch)` method supports the full range of Unicode characters. It handles not only the standard English alphabet (A-Z) but also accented characters and alphabets from other languages (e.g., Cyrillic, Greek). 2. **Supplementary Characters**: The `char` data type in Java represents a 16-bit UTF-16 code unit. For supplementary Unicode characters (which require 32 bits), you should use the overloaded method `Character.toLowerCase(int codePoint)` instead. 3. **String Alternative**: If you need to convert an entire string to lowercase, it is generally more efficient and convenient to use the `String.toLowerCase()` method rather than iterating through characters manually.
← Java String ComparetoignorecasCharacter Iswhitespace β†’