Java Character Class – 8 Astonishing Methods with Syntax & Examples

Character class in Java has the function of wrapping a primitive type variable into a character object. However this should not be confused with the character datatype that we learned about in the previous articles. Java Character Class is a predefined class with a predefined set of methods like isLetter, isDigit etc.

Java character class

Keeping you updated with latest technology trends, Join DataFlair on Telegram

Java Character Class

Character Class is a wrapper class. A wrapper class in its simplest form is a class that wraps(converts) a primitive datatype to an object. Why do we need to do that? Sometimes we need to change or apply different methods to a character variable. We cannot do that unless we convert the value into an immutable object. The java.util package handles wrapper classes.

Character class in Java is immutable. Once the value of a character datatype is changed to a character object it cannot be reverted back to its form.

The basic syntax for converting a character variable to a Character object is:

Character <variable name> = new Character(<value>);

Example:

Character c = new Character(‘z’);

This converts the character c into a Character object with the value of the argument passed as ‘z’.

Some frequently used Character Class methods used in Java are:

Character class methods in java

1. isLetter Method in Java

This method returns a boolean value, i.e true or false. It returns true if the argument passed in the method is a letter i.e any character in between a-z and A-Z. Otherwise it returns false.

However we can also include the ascii value of the character instead of passing the character directly as an argument because Java implicitly converts all characters to its ASCII value.

Syntax:

boolean isLetter(<character>);

Example program to illustrate the use of Java isLetter method:

package com.dataflair.characterclass;
import java.util. * ;
public class IsLetter {
  public static void main(String[] args) {
    System.out.println("The value of isLetter('a') is " + Character.isLetter('a'));
    System.out.println("The value of isLetter('7') is " + Character.isLetter('7'));
    System.out.println("The value of isLetter('97') is " + Character.isLetter(97));
  }

}

Output

The value of isLetter(‘a’) is true
The value of isLetter(‘7′) is false
The value of isLetter(’97’) is true

Note: Although 97 is not a letter it is the ASCII value of ‘a’ which is a letter. Hence it evaluates to true. Whereas the number 7 neither is a letter nor the ASCII value of a letter. Hence it evaluates to false.

2. isDigit Method in Java

The isDigit method as the name suggests checks whether the parameter passed to it is a digit or not. It returns true if the argument passed is a digit. Else it returns false.

This method is particularly used in situations where we need to filter data based on its numeric or character value.

Syntax:

boolean isDigit(<character>);

An example program to illustrate the use of Java isDigit method:

package com.dataflair.characterclass;
import java.util. * ;
public class IsDigit {
  public static void main(String[] args) {
    System.out.println("The value of isDigit('b') is " + Character.isDigit('a'));
    System.out.println("The value of isDigit('7') is " + Character.isDigit('7'));
    System.out.println("The value of isDigit('8') is " + Character.isDigit('8'));
  }

}

Output

The value of isDigit(‘b’) is false
The value of isDigit(‘7’) is true
The value of isDigit(‘8’) is true

3. isWhitespace Method in Java

Sometimes when the value of a character is a space we need to know that it exists in a string. Hence we need the method of isWhitespace. It checks whether the value of the argument passed is a white space or not. However this method also returns true if the argument is a tab or new line.

Syntax:

boolean isWhitespace(<character>);

Java program to illustrate the use of Java isWhitespace method:

package com.dataflair.characterclass;
import java.util. * ;
public class IsWhitespace {
  public static void main(String[] args) {
    System.out.println("The value of isWhitespace(' ') is " + Character.isWhitespace(' '));
    System.out.println("The value of isWhitespace('\\n') is " + Character.isWhitespace('\n'));
    System.out.println("The value of isWhitespace('\\t') is " + Character.isWhitespace('\t'));
    System.out.println("The value of isWhitespace('b') is " + Character.isWhitespace('b'));
  }

}

Output

The value of isWhitespace(‘ ‘) is true
The value of isWhitespace(‘\n’) is true
The value of isWhitespace(‘\t’) is true
The value of isWhitespace(‘b’) is false

4. isUppercase Method in Java

This method is primarily used for checking whether the character passed in the argument is an UpperCase character or not. It returns true if the character is in Uppercase. If not, it returns false.

Syntax:

boolean isUpperCase(<character>);

Java Program to illustrate the use of Java isUpperCase method:

package com.dataflair.characterclass;
import java.util. * ;
public class IsUpperCase {
  public static void main(String[] args) {
    System.out.println("The value of isUpperCase('a') is " + Character.isUpperCase('a'));
    System.out.println("The value of isUpperCase('B') is " + Character.isUpperCase('B'));
    System.out.println("The value of isUpperCase('c') is " + Character.isUpperCase('c'));
    System.out.println("The value of isUpperCase('D') is " + Character.isUpperCase('D'));
  }

}

Output

The value of isUpperCase(‘a’) is false
The value of isUpperCase(‘B’) is true
The value of isUpperCase(‘c’) is false
The value of isUpperCase(‘D’) is true

5. isLowercase Method in Java

This method is primarily used for checking whether the character passed in the argument is a LowerCase character or not. It returns true if the character is in LowerCase. If not, it returns false.

Syntax:

boolean isLowerCase(<character>);

Java Program to illustrate the use of the isLowerCase method:

package com.dataflair.characterclass;
import java.util. * ;
public class IsLowerCase {
  public static void main(String[] args) {
    System.out.println("The value of isLowerCase('a') is " + Character.isLowerCase('a'));
    System.out.println("The value of isLowerCase('B') is " + Character.isLowerCase('B'));
    System.out.println("The value of isLowerCase('c') is " + Character.isLowerCase('c'));
    System.out.println("The value of isLowerCase('D') is " + Character.isLowerCase('D'));
  }

}

Output

The value of isUpperCase(‘a’) is true
The value of isUpperCase(‘B’) is false
The value of isUpperCase(‘c’) is true
The value of isUpperCase(‘D’) is false

6. toUpperCase Method in Java

This method, unlike all the methods we have studied until now, is a character returning method. This takes the argument and converts it into an uppercase alphabet. It returns an uppercase alphabet.

Syntax:

character toUpperCase(<character/ASCII value>);

Program to illustrate the use of Java toUpperCase method:

package com.dataflair.characterclass;
import java.util. * ;
public class ToUpperCase {
  public static void main(String[] args) {
    System.out.println("The value of toUpperCase('a') is " + Character.toUpperCase('a'));
    System.out.println("The value of toUpperCase('B') is " + Character.toUpperCase('B'));
    System.out.println("The value of toUpperCase('c') is " + Character.toUpperCase('c'));
    System.out.println("The value of toUpperCase(112) is " + Character.toUpperCase(112));
  }

}

Output

The value of toUpperCase(‘a’) is A
The value of toUpperCase(‘B’) is B
The value of toUpperCase(‘c’) is C
The value of toUpperCase(112) is 80

Note: When a numeric value 112 which is the ASCII value of ‘p’ is passed it returns an ASCII value of the letter ‘P’.

7. toLowerCase Method in Java

Java toLowerCase method converts the arguments passed to it into lowercase. However, if a numeric value i.e ASCII value of an UpperCase letter is passed through it returns the ASCII value of the corresponding lowercase alphabet.

Syntax:

character toLowerCase(<character/ ASCII value>);

Java program to illustrate the use of toLowerCase method:

package com.dataflair.characterclass;
import java.util. * ;
public class ToLowerCase {
  public static void main(String[] args) {
    System.out.println("The value of toLowerCase('a') is " + Character.toLowerCase('a'));
    System.out.println("The value of toLowerCase('B') is " + Character.toLowerCase('B'));
    System.out.println("The value of toLowerCase('c') is " + Character.toLowerCase('c'));
    System.out.println("The value of toLowerCase(80) is " + Character.toLowerCase(80));
  }

}

Output

The value of toLowerCase(‘a’) is a
The value of toLowerCase(‘B’) is b
The value of toLowerCase(‘c’) is c
The value of toLowerCase(80) is 112

Note: The value 80 passed is actually the ASCII value of ‘P’ which when returned as ‘p’ proves the working of the method toLowerCase.

8. toString Method in Java

This method has the responsibility of converting the character arguments passed through it, to String objects for manipulation as and when required by the programmer. ASCII values cannot be passed as arguments here.

Syntax:

String toString(<character>);

Java program to illustrate the use of the toString method:

package com.dataflair.characterclass;
import java.util. * ;
public class ToString {
  public static void main(String[] args) {
    System.out.println("The value of toString('a') is " + Character.toString('a'));
    System.out.println("The value of toString('B') is " + Character.toString('B'));
    System.out.println("The value of toString('c') is " + Character.toString('c'));
  }

}

Output

The value of toString(‘a’) is a
The value of toString(‘B’) is B
The value of toString(‘c’) is c

Escape Sequences in Java

There are some particular characters, which when preceded with a backslash(\), holds special meaning to the compiler. Some of the escape sequences are:

  • \n- creates a new line
  • \t- creates a tab space
  • \b- inserts a single backspace
  • \r-inserts carriage return
  • \\- inserts a backslash character
  • \’- inserts single quote character
  • \” – inserts double-quote character

Summary

Finally we learnt that Java character class is one of the most important wrapper classes in Java which is used for a lot of manipulations. We also came to know about escape sequences which would help in outputs of our code to a great extent. A clear concept of these is of utmost importance in programming.

Hope you liked this java tutorial. Do rate DataFlair at Google to let us know your experience.

1 Response

  1. Supriyo Das Adhikary says:

    Thank you very much for these informations. For this information, I am able to complete my computer project. Thank you very much again 😄😘😭😭😭😭😭

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.