Categories: Java

Character class in Java

In java.lang package, Java includes a wrapper class called Character. A single field of type char is contained in a Character object. The Character class provides several useful static (class) methods for manipulating characters. The Character constructor can be used to create a Character object.

Constructor of Character wrapper class:
Character(char c)
Methods of Character Wrapper class:
MethodsDescription
static boolean isDigit(char ch)If ch is a digit, returns true; otherwise, returns false.
static boolean isLetter(char ch)If ch is a letter, returns true; otherwise, returns false.
static boolean IsLetterOrDigit(char ch)If ch is either a letter or a digit, returns true; otherwise, returns false.
static boolean isLowerCase(char ch)If ch is a lowercase letter, returns true; otherwise, returns false.
static boolean isUpperCase(char ch)If ch is an uppercase letter, returns true; otherwise, returns false.
static boolean iswhite space(char ch)If ch is a white space character, returns true; otherwise, returns false.
static char toLowerCase(char ch)Returns the lowercase form of ch.
static char toUpperCase(char ch)Returns an uppercase form of ch.
int charCount(int codePoint) Returns the number of char values required to represent the specified character.
codePointAt(char[]a, int index)returns the codePoint for the given array’s index.
codePointBefore(char[]a, int index)returns the codePoint in the preceding index for the given array.
codePointOf(String name)returns the codePoint value for the given Unicode character specified by a character name.
codePointCount(char[]a, int offset, int count)returns the total number of Unicode codePoints in the char array argument’s given subarray.
digit(char ch, int radix)returns the numeric value for the specified character at the given index.
equals(object obj)It compares the specified object to the given object.
forDigit(int digit, int radix)It determines how the character is displayed for a specific digit in the given radix.
getDirectionality(char ch)The Unicode directionality property for the specified character is returned by getDirectionality(char ch).
Example: Illustrating the use of various methods
import java.util.Scanner;  
public class Coderz
{  
   public static void main(String[] args)
   {  
    // Ask the user for the first input.   
       System.out.print("Enter the first input:");  
    // Use the Scanner class to get the user input.  
       Scanner scanner = new Scanner(System.in);  
    // Gets the user input.  
       char[] value1 = scanner.nextLine().toCharArray();  
       int result1 = 0;  
    // Count the characters for a specific character.  
       for (char ch1 : value1)
       {  
       result1 = Character.charCount(ch1);  
       }  
    // Print the result.  
        System.out.print("The value comes to: "+result1+"\n");  
         
       System.out.print("Enter the second input:");  
       char[] value2 = scanner.nextLine().toCharArray();  
        for (char ch2 : value2) 
        {  
        int result2 = Character.hashCode(ch2);  
       System.out.print("The hash code for the character '"+ch2+"' is given as:"+result2+"\n");  
       }  
      System.out.print("Enter the third input:");  
       char[] value3 = scanner.nextLine().toCharArray();  
       for (char ch3 : value3)
       {  
      boolean result3 = Character.isDigit(ch3);  
      if(result3)
      {  
System.out.println("The character '" + ch3 + "' is a digit. ");               
                }  
      else
      {  
System.out.println("The character '" + ch3 + "' is not a digit.");  
           }      
      System.out.print("Enter the fourth input:");  
       char[] value4 = scanner.nextLine().toCharArray();  
       for (char ch4 : value4) 
       {  
      boolean result4 = Character.isISOControl(ch4);  
      System.out.println("The fourth character '"+ch4+"' is an ISO Control:"+result4);                
         }  
       }  
   }  
}  
Output:
Enter the first input:hi
The value comes to: 1
Enter the second input:23
The hash code for the character '2' is given as:50
The hash code for the character '3' is given as:51
Enter the third input:,
The character ',' is not a digit.
Enter the fourth input:/
The fourth character '/' is an ISO Control:false
Escape Sequences:

An escape sequence is a character that is preceded by a backslash () and has special meaning to the compiler. The Java escape sequences are listed in the table below:

Escape SequenceDescription
\tInsert a tab in the text at this point.
\bInsert a backspace in the text at this point.
\nInsert a new line in the text at this point.
\rInsert a carriage return in the text at this point.
\fInsert a formfeed in the text at this point.
\’Insert a single quote character in the text at this point.
\”Insert a double quote character in the text at this point.
\\Insert a backslash character in the text at this point.
Example:
class Coderz {
    public static void main(String[] args) {
        System.out.println("Student said \"Good Morning!\" to me.");
    }
}
Output:
Student said "Good Morning!" to me.

Note: also read about the Boolean class in Java

Follow Me

If you like my post, please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

2 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago