Java - Character toLowerCase() Method



The Java Character toLowerCase() method converts the character (Unicode code point) argument to lowercase using case mapping information from the UnicodeData file.

According to UnicodeData file, case is defined as the inherent property of a character. Case mappings in this file are informative and default mappings. For example, if a character is CAPITAL by default, then its corresponding lowercase is informative.

This method does not convert all characters in Unicode to their lowercase; especially characters like symbols or ideographs.

Note − There are two polymorphic forms of this method, with different parameter types.

Syntax

Following is the syntax for Java Character toLowerCase() method

public static int toLowerCase(int codePoint)
(or)
public static char toLowerCase(char ch)

Parameters

  • codePoint − the Unicode code point to be converted

  • ch − the character to be converted

Return Value

This method returns the lowercase equivalent of the character or Unicode code point, if any; otherwise, the character itself.

Example

The following example shows the usage of Java Character toLowerCase(int codePoint) method.

package com.tutorialspoint;
import java.lang.*;
public class CharacterDemo {
   public static void main(String[] args) {

      // create 4 int primitives
      int cp1, cp2, cp3, cp4;

      // assign values to cp1, cp2
      cp1 = 0x0057;
      cp2 = 0x2153;

      // assign lowercase of cp1, cp2 to cp3, cp4
      cp3 = Character.toLowerCase(cp1);
      cp4 = Character.toLowerCase(cp2);
      String str1 = "Lowercase equivalent of cp1 is " + cp3;
      String str2 = "Lowercase equivalent of cp2 is " + cp4;

      // print cp3, cp4 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Lowercase equivalent of cp1 is 119
Lowercase equivalent of cp2 is 8531

Example

The following example shows the usage of Java Character toLowerCase(char ch) method.

package com.tutorialspoint;
import java.lang.*;
public class CharacterDemo {
   public static void main(String[] args) {

      // create 4 char primitives
      char ch1, ch2, ch3, ch4;

      // assign values to ch1, ch2
      ch1 = 'P';
      ch2 = 's';

      // assign lowercase of ch1, ch2 to ch3, ch4
      ch3 = Character.toLowerCase(ch1);
      ch4 = Character.toLowerCase(ch2);
      String str1 = "Lowercase of " + ch1 + " is " + ch3;
      String str2 = "Lowercase of " + ch2 + " is " + ch4;

      // print ch3, ch4 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Lowercase of P is p
Lowercase of s is s

Example

In the following example, we write a program that uses conditional statements to check whether the arguments of the method can be converted to lowercase letters or not.

import java.lang.*;
public class CharacterDemo {
   public static void main(String[] args) {
      char ch, ch2;      
        ch = '4';        
        if(Character.getType(ch) == 1 || Character.getType(ch) == 2){
           ch2 = Character.toLowerCase(ch);
           System.out.println("Lowercase of " + ch + " is " + ch2);
        }
        else
           System.out.println("The character cannot be converted to Lowercase");
   }
}

Output

Compile and run the given program above to obtain the output as follows −

The character cannot be converted to Lowercase

Example

As we have discussed in ths article earlier, the method does not return true for some characters like symbols and ideographs. We will understand the scenario in the following example program.

import java.lang.*;
public class Main {
   public static void main(String[] args) {
      char ch, result;      
      ch = '%';
      result = Character.toLowerCase(ch);
      System.out.println("Lowercase of " + ch + " is " + result);
   }
}

Output

The output for the above program will be printed as follows −

Lowercase of % is %
java_lang_character.htm
Advertisements