java.lang.Character class – methods

lang.Character methods


java.lang.Character class – methods | Set 1

Following methods of Character class are discussed here :

    1. toUpperCase() : java.lang.toUpperCase(char arg) method converts given character in argument to its Upper case based on Unicode values.
      Syntax :

      public static char toUpperCase(char arg)
      Parameters : 
      arg : character to be converted to Upper case
      Return : 
      Upper case character of the argumented character.  
      
    2. toLowerCase() : java.lang.toLowerCase(char arg) method converts given character in argument to its Lower case based on Unicode values.
      Syntax :

      public static char toLowerCase(char arg)
      Parameters : 
      arg : character to be converted to Lower case
      Return : 
      Lower case character of the argumented character.  
      
    3. isMirrored() : java.lang.Character.isMirrored(char arg) method checks whether passed argument is mirrored or not, based on Unicode values. Mirrored characters should have their glyphs horizontally mirrored when displayed in text that is right-to-left. For example, ‘u0028’ LEFT PARENTHESIS is semantically defined to be an opening parenthesis. This will appear as a “(” in text that is left-to-right but as a “)” in text that is right-to-left.
      e.g. : [ ] { } ( )
      Syntax :

      public static boolean isMirrored(char arg)
      Parameters : 
      arg : argumented character 
      true if char 'arg' is mirrored, else false.
      

Java code explaining use of toUpperCase(), toLowerCase(), isMirrored() methods

// Java program explaining Character class methods
// toUpperCase(), toLowerCase(), isMirrored()
import java.lang.Character;
public class NewClass
{
    public static void main(String[] args)
    {
        // Use of toUpperCase() method
        Character g1 = new Character('g');
        Character g2 = new Character('O');
        char chUpper = Character.toUpperCase(g1);
        char chUpper1 = Character.toUpperCase(g2);
        System.out.println("Upper value for g : "+ chUpper);
        System.out.println("Upper value for O : "+ chUpper1);
        System.out.println("");        
        // Use of toLowerCase() method
        char chLower = Character.toLowerCase(g1);
        char chLower1 = Character.toLowerCase(g2);
        System.out.println("Lower value for G : "+ chLower);
        System.out.println("Lower value for O : "+ chLower1);
        System.out.println("");
        // Use of isMirrored() method
        Character g3 = new Character('-');
        Character g4 = new Character(')');
        Character g5 = new Character('{');
        Character g6 = new Character(']');
        boolean checkBool1 = Character.isMirrored(g3);
        boolean checkBool2 = Character.isMirrored(g4);
        boolean checkBool3 = Character.isMirrored(g5);
        boolean checkBool4 = Character.isMirrored(g6);
        System.out.println("Checking - : "+ checkBool1);
        System.out.println("Checking ) : "+ checkBool2);
        System.out.println("Checking { : "+ checkBool3);
        System.out.println("Checking ] : "+ checkBool4);
    }
}

Output:

Upper value for g : G
Upper value for O : O
Lower value for G : g
Lower value for O : o
Checking - : false
Checking ) : true
Checking { : true
Checking ] : true
    1. reverseBytes() : java.lang.Character.reverseBytes() method returns a character by reversing the order of bytes in the argumented character.
      Syntax :

      public static char reverseBytes()
      Parameters : 
      cP : code point, need to check
      Return : 
      character with reversed order of bytes in the argumented character
      
    2. isAlphabetic() : java.lang.Character.isAlphabetic(int cP) method checks whether the argumented character(code point) is an alphabet or not
      Syntax :

      public static boolean isAlphabetic(int codePoint)
      Parameters : 
      cP : code point, need to check.  
      Return : 
      True if character is a Unicode alphabet, else false
      Exception : 
      --> NullPointerException
      --> IndexOutOfBoundsException 
      
    3. isValidCodePoint() : java.lang.Character.isValidCodePoint( int cP) method checks whether the argumented Unicode is actually valid or not
      Syntax :

      public static boolean isValidCodePoint(int codePoint)
      Parameters : 
      cP : code point to be tested  
      Return : 
      true if Min code point <  Argumented code point < Max code point
      

Java code explaining use of isValidCodePoint(), reverseBytes(), isAlphabetical() methods

// Java program explaining Character class methods
// isValidCodePoint(), reverseBytes(), isAlphabetical()
import java.lang.Character;
public class NewClass
{
    public static void main(String[] args)
    {
        // Use of reverseBytes() method
        Character g1 = new Character('1');
        Character g2 = new Character('s');
        char chreverse = Character.reverseBytes(g1);
        char chreverse1 = Character.reverseBytes(g2);
        System.out.println("Reverse char for 1 : "+ chreverse);
        System.out.println("Revrese char for s : "+ chreverse1);
        System.out.println("");
        // Use of isAlphabetical() method
        int c1 = 101, c2 = 132;
        boolean check1 = Character.isAlphabetic(c1);
        boolean check2 = Character.isAlphabetic(c2);
        System.out.println("Is Unicode 66 alphabetic  : "+ check1);
        System.out.println("Is Unicode 132 alphabetic : "+ check2);
        System.out.println("");
        // Use of isValidCodePoint() method
        int c3 = 0x012343, c4 = 0x01344ffff;
        boolean check4 = Character.isValidCodePoint(c4);
        boolean check3 = Character.isValidCodePoint(c3);
        System.out.println("Validity check : "+ check3);
        System.out.println("Validity check : "+ check4);
    }
}

Output:

Reverse char for 1 : ?
Reverse char for s : ?
Is Unicode 66 alphabetic  : true
Is Unicode 132 alphabetic : false
Validity check : true
Validity check : false

Leave a Reply