
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Character Wrapper Class and Its Methods in Java
In this article, we will learn about the character wrapper class and its methods in Java. The Character class is the primitive char type wrapper, providing useful methods for manipulation, classification, and conversion of characters.
The Character Wrapper Class
The Character class of the java.lang package wraps a value of the primitive datatype char. It offers a number of useful class (i.e., static) methods for manipulating characters. You can create a Character object with the Character constructor.
Syntax
Character ch = new Character('a');
Various Methods in the Character Class
The following are some of the methods used in the Character class ?
1 |
Determines whether the specified char value is a letter. |
2 |
Determines whether the specified char value is a digit. |
3 |
Determines whether the specified char value is white space. |
4 |
Determines whether the specified char value is uppercase. |
5 |
Determines whether the specified char value is lowercase. |
6 |
Returns the uppercase form of the specified char value. |
7 |
Returns the lowercase form of the specified char value. |
8 |
Returns a String object representing the specified character value that is, a one-character string. |
Using the isDigit() Method
The following are the steps to check whether the character is a digit or not using the isDigit() method ?
-
Declaring and Initializing Characters: ch1 = '9' and ch2 = 'V' represent two different character values.
-
Checking if Characters Are Digits:
-
Character.isDigit(ch1) returns true because '9' is a numeric digit.
- Character.isDigit(ch2) returns false because 'V' is not a digit.
-
Character.isDigit(ch1) returns true because '9' is a numeric digit.
b1 = Character.isDigit(ch1); b2 = Character.isDigit(ch2);
Example
Below is an example to check whether the character is a digit or not ?
public class CharacterClassExample { public static void main(String[] args) { char ch1, ch2; ch1 = '9'; ch2 = 'V'; boolean b1, b2; b1 = Character.isDigit(ch1); b2 = Character.isDigit(ch2); String str1 = ch1 + " is a digit is " + b1; String str2 = ch2 + " is a digit is " + b2; System.out.println( str1 ); System.out.println( str2 ); } }
Output
9 is a digit is true V is a digit is false
Time complexity: O(1) as Character.isDigit() runs in constant time.
Space complexity: O(1) since only a few variables are used.