Character class Methods Java

Java Character class comes with many static methods with which a given character can be tested for a criteria like it is upper case letter or lower case letter or a digit etc. It is placed by Designers in java.lang package so that is it is implicitly imported and available fro each program you write.

Following example uses often and less used Character class methods.
public class CharacterClassMethods
{
  public static void main(String args[])
  {					
    char c1 = 'X';    
    char c2 = '9';   

    System.out.println("c1 is : " + c1);        		// prints X
    System.out.println("c2 is : " + c2);        		// prints 9

    // Rarely used methods - to check character can be used as Java identifier
    
    System.out.println("X can be starting letter in Java identifier: " + Character.isJavaIdentifierStart(c1)); // true
    System.out.println("9 can be starting letter in Java identifier: " + Character.isJavaIdentifierStart(c2));  // false
    System.out.println("9 can be put anywhere except starting letter in Java identifier: " + Character.isJavaIdentifierPart(c2));   // true

    // Frequently used methods for validations - to check char properties   
        
    System.out.println("X is defined: " + Character.isDefined(c1));     		// true
    System.out.println("X is digit: " + Character.isDigit(c1));    			// false
    System.out.println("X is letter (alphabet): " + Character.isLetter(c1));   	// true
    System.out.println( "X is letter or digit: " + Character.isLetterOrDigit(c2));  // true
    System.out.println( "X is lower case: " + Character.isLowerCase(c1));     	// false
    System.out.println( "X is upper case: " + Character.isUpperCase(c1));  	// true
    System.out.println( "X to lower case: " + Character.toLowerCase(c1));   	// prints x
    System.out.println( "X is space: " + Character.isWhitespace(c1));   	// false
  }
}

Character class Methods JavaOutput Screen on Character class Methods Java

  1. isJavaIdentifierStart(c1) checks whether c1 can be the starting letter of a Java identifier. We know in rules of Java Identifiers, the starting letter should be a letter or an underscore or $ symbol. Starting letter cannot be a digit.
  2. isJavaIdentifierPart(c2) checks whether c2 can be used in identifier (apart as starring letter). We know the other characters except the starting one can be digit also.
  3. isDefined(c1) checks whether c1 is defined in Java Unicode character set.

In the earlier code, we used data type char. But Java comes with Character class also that represents char as an object. Character class is a Wrapper class. Let us see how to use Character class for comparisons; important for a Developer.

public class CharacterClassMethods
{
  public static void main(String args[])
  {					
    Character achar = new Character('a');
    Character achar1 = new Character('a');
    Character bchar = new Character('b');
                                               // using directly Character objects
    System.out.println("c1 and c2 with compareTo(): " + achar.compareTo(achar1));    // prints 0 (97 - 97)
    System.out.println("xchar and ychar same or not: " + achar.compareTo(bchar));    // prints -1 (97 - 98)

    System.out.println("xchar and ychar same or not: " + achar.equals(bchar));       // prints false

   				               // to retrieve char data type from Character object
       char c1 = achar.charValue();  
       System.out.println("a is letter (alphabet): " + Character.isLetter(c1));      // true
    }                                          // Now with c1, you can use all the earlier code methods.  
}

ssOutput Screen on Character class Methods Java

  1. achar.compareTo(bchar) does lexicographical comparison. It compares achar and bchar ASCII values and returns their difference as (ASCII of achar – ASCII of bchar). If both achar and bchar are same, returns 0.

Let us have a small practical example on validations. The constraint checked is the password should have only lowercase letters, should not be a digit or whitespace.

Following example checks for Password constraints.
public class CharacterClassMethods
{
  public static void main(String args[])
  {					
    String password = "hello9";  // can be taken in realtime from GUI

    for(int i = 0; i< password.length(); i++)
    {
      char c1 = password.charAt(i);
      if( Character.isDigit(c1) ||  Character.isWhitespace(c1)  || Character.isUpperCase(c1)  )
      {
        System.out.println("Not Valid");
        break;
      }
    }
  }                                                            
}

ssOutput Screen on Character class Methods Java

More on Character class discussion is available at class Character

Leave a Comment

Your email address will not be published.