Wednesday 5 April 2023

Java Character Wrapper class and Its methods

In Java, the Character class is the wrapper class for the char primitive data type. The Character class provides a range of methods to work with char values, including:

  1. isDigit(char ch): Returns true if the specified char value is a digit.

  2. isLetter(char ch): Returns true if the specified char value is a letter.

  3. isWhitespace(char ch): Returns true if the specified char value is a whitespace character.

  4. toLowerCase(char ch): Returns the lowercase equivalent of the specified char value.

  5. toUpperCase(char ch): Returns the uppercase equivalent of the specified char value.

  6. valueOf(char ch): Returns a Character instance representing the specified char value.

  7. compareTo(Character anotherChar): Compares this Character instance with another Character instance.

  8. equals(Object obj): Returns true if the specified object is equal to this Character instance.

The Character class also provides constants for commonly used char values, such as Character.MAX_VALUE and Character.MIN_VALUE.

The Character class is commonly used in applications that deal with text and character data. For example, it can be used to check whether a given character is a digit or a letter, or to convert a character to its lowercase or uppercase equivalent.

Here's an example Java program that demonstrates the use of some methods of the Character class:

  

public class CharacterExample {
    public static void main(String[] args) {
        char ch = 'A';

        // Check if the character is a letter
        if (Character.isLetter(ch)) {
            System.out.println(ch + " is a letter");
        }

        // Convert the character to lowercase
        char lowercase = Character.toLowerCase(ch);
        System.out.println("Lowercase of " + ch + " is " + lowercase);

        // Convert the character to uppercase
        char uppercase = Character.toUpperCase(ch);
        System.out.println("Uppercase of " + ch + " is " + uppercase);
    }
}
 

In this program, we first declare a char variable ch and initialize it with the character 'A'. We then use the isLetter method of the Character class to check whether ch is a letter, and print a message accordingly.

Next, we use the toLowerCase method of the Character class to convert ch to its lowercase equivalent, and print the result. Similarly, we use the toUpperCase method of the Character class to convert ch to its uppercase equivalent, and print the result.

0 comments :

Post a Comment

Note: only a member of this blog may post a comment.

Machine Learning

More

Advertisement

Java Tutorial

More

UGC NET CS TUTORIAL

MFCS
COA
PL-CG
DBMS
OPERATING SYSTEM
SOFTWARE ENG
DSA
TOC-CD
ARTIFICIAL INT

C Programming

More

Python Tutorial

More

Data Structures

More

computer Organization

More
Top