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:
Methods | Description |
---|---|
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 Sequence | Description |
---|---|
\t | Insert a tab in the text at this point. |
\b | Insert a backspace in the text at this point. |
\n | Insert a new line in the text at this point. |
\r | Insert a carriage return in the text at this point. |
\f | Insert 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
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Leave a Comment