Java char value and its attributes

In this chapter you will learn:

  1. How to Check the value of a character type
  2. How to Get the char type from char value
  3. How to Change character case
  4. How to compare two characters

Check the value of a character type

A char type value can store a letter or a digit. The letter can be upper case or lower case. It can also store unicode. The following list of methods are the ones we are going to use to check the char type value.

  • static boolean isDefined(char ch) checks if a character is defined in Unicode.
  • static boolean isDigit(char ch) checks if the character is a digit.
  • static boolean isHighSurrogate(char ch) checks if the char is a high-surrogate code unit.
  • static boolean isIdentifierIgnorable(char ch) returns if the character should be regarded as an ignorable character in a Java identifier or a Unicode identifier.
  • static boolean isISOControl(char ch) returns if the character is an ISO control character.
  • static boolean isJavaIdentifierPart(char ch) returns if the character may be part of a Java identifier as other than the first character.
  • static boolean isJavaIdentifierStart(char ch) returns if the character is permissible as the first character in a Java identifier.
  • static boolean isLetter(char ch) returns if the character is a letter.
  • static boolean isLetterOrDigit(char ch) returns if the character is a letter or digit.
  • static boolean isLowerCase(char ch) returns if the character is a lowercase character.
  • static boolean isLowSurrogate(char ch) returns if the char value is a low-surrogate code unit.
  • static boolean isMirrored(char ch) returns whether the character is mirrored according to the Unicode specification.
  • static boolean isSpaceChar(char ch) returns if the character is a Unicode space character.
  • static boolean isSurrogatePair(char high, char low) returns whether the pair of char values is a valid surrogate pair.
  • static boolean isTitleCase(char ch) returns if the character is a titlecase character.
  • static boolean isUnicodeIdentifierPart(char ch) returns if the character may be part of a Unicode identifier as other than the first character.
  • static boolean isUnicodeIdentifierStart(char ch) returns if the character is permissible as the first character in a Unicode identifier.
  • static boolean isUpperCase(char ch) returns if the character is an uppercase character.
  • static boolean isWhitespace(char ch) returns if the character is white space according to Java.

A demo for some of the methods listed above.

public class Main{
  public static void main(String[] argv){
    System.out.println("is digit:"+Character.isDigit('a'));    
    System.out.println("is letter:"+Character.isLetter('a'));    
    System.out.println("is lowercase:"+Character.isLowerCase('a'));    
    System.out.println("is space char:"+Character.isSpaceChar('a'));    
    System.out.println("is white space:"+Character.isWhitespace('a'));    
    System.out.println("is upper case:"+Character.isUpperCase('a'));    
    System.out.println("is Java identifier start:"+Character.isJavaIdentifierStart('a'));    
/*j a  va  2s . c om*/
  }
}

The output:

The following code uses a for loop to check the each value in a char array.

public class Main {
  public static void main(String args[]) {
    char a[] = {'D', 'e', 'm', 'O', '2', '.','c','O','M'};
/* j ava 2  s. c  o m*/
    for (int i = 0; i < a.length; i++) {
      if (Character.isDigit(a[i]))
        System.out.println(a[i] + " is a digit.");
      if (Character.isLetter(a[i]))
        System.out.println(a[i] + " is a letter.");
      if (Character.isWhitespace(a[i]))
        System.out.println(a[i] + " is whitespace.");
      if (Character.isUpperCase(a[i]))
        System.out.println(a[i] + " is uppercase.");
      if (Character.isLowerCase(a[i]))
        System.out.println(a[i] + " is lowercase.");
    }
  }
}

The output:

D is a letter./*from j  av a  2  s . co m*/
D is uppercase.
e is a letter.
e is lowercase.
m is a letter.
m is lowercase.
O is a letter.
O is uppercase.
2 is a digit.
c is a letter.
c is lowercase.
O is a letter.
O is uppercase.
M is a letter.
M is uppercase.

Get the char type from char value

static int getType(char ch) returns a value indicating a character's general category.

public class Main{
  public static void main(String[] argv){
    System.out.println(Character.getType('a'));    
/*  j av a 2  s  .  c  o  m*/
  }
}

The output:

Change character case

  • static char toTitleCase(char ch) converts to titlecase.
  • static char toUpperCase(char ch) converts to uppercase.
  • static char toLowerCase(char ch) converts to lowercase.
public class Main{
  public static void main(String[] argv){
    System.out.println(Character.toTitleCase('a'));    
    System.out.println(Character.toUpperCase('a'));    
    System.out.println(Character.toLowerCase('a'));    
/*from j a  va 2  s . c om*/
  }
}

The output:

Compare two characters

  • int compareTo(Character anotherCharacter) compares two Character objects numerically.
  • boolean equals(Object obj) compares this object against the specified object.
public class Main {
  public static void main(String[] argv) {
    Character character1 = new Character('a');
    Character character2 = new Character('b');
    System.out.println(character1.compareTo(character2));
  }//  j a va2  s.  c  om
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. What is Java boolean type
  2. How to create boolean Literals
  3. How to create Boolean value object with constructors
  4. How to Get boolean value from constants in Boolean class
  5. How to Compare two boolean values
Home » Java Tutorial » Data Types

Java Primitive Data Types
Java byte type
Java byte type conversion
Java short type
Java short type conversion
Java int type
Java int type conversion
Java long type
Java long type conversion
Java float type
Java float type conversion
Java double type
Java double type creation and comparison
Java double type conversion
Java Automatic Type Conversion and Casting
Data type casting
Java type promotion
Java char type
Java char conversion
Java char value and its attributes
Java boolean type
Java boolean type conversion
Autoboxing and auto-unboxing
Java Array
Create an Array
Array Index and length
Multidimensional Arrays
Array examples
Array copy
Array compare
Array Binary search
Array sort
Array to List
Array fill value
Array to String
BigInteger class
BigInteger creation
BigInteger add, subtract, multiply and divide
BigInteger power and modPow
BigInteger conversion
BigInteger to String
BigInteger bit and,or,xor,test,flip,negate
BigInteger bit shift left and right
BigInteger prime value
BigDecimal
BigDecimal constants
BigDecimal Rounding mode
BigDecimal creation
BigDecimal calculation
BigDecimal convert
BigDecimal Comparison
BigDecimal to String
BigDecimal decimal point
BigDecimal precision
BigDecimal format
Currency class