Character Wrapper Class


Java / Wednesday, August 8th, 2018

Java Character class and methods

Java Character class and methods are placed in java.lang package. It is a wrapper class. Sometimes programmer needs to use objects instead of primitive data types. Character wrapper class is for primitive data type char. Package java.lang is implicitly available to the user to write code.

Character Methods

These methods deal only with character manipulation in java. Some of them are explained below:

Character.isLetter()

This method is used to check whether a given argument is a letter or not. It returns a Boolean type value either true or false.
Syntax:
boolean var_name = Character.isLetter(char_name);
Or, boolean var_name = Character.isLetter(character);
Example:
boolean k = Character.isLetter(‘m’);
The method will return true to the variable k.
boolean k = Character.isLetter(‘8’);
It will return false.

Character.isDigit()

This method returns a boolean  type value true if a given argument is a digit, otherwise false.
Syntax:
boolean var_name = Character.isDigit(char_name);
Or, boolean var_name = Character.isDigit(character);
Example:
boolean k = Character.isDigit(‘8’);
It returns true to the variable k.
boolean k = Character.isDigit(‘M’);
It returns false to the variable k.

Character.isLetterOrDigit()

This method returns true if the given argument is either a letter or a digit, false otherwise.
Syntax:
boolean var_name = Character.isLetterOrDigit(char_name);
Or, boolean var_name = Character.isLetterOrDigit(character);
Example:
boolean k =  Character.isLetterOrDigit(‘G’);
It returns true to the boolean variable k as ‘G’ is a letter.
boolean k =  Character.isLetterOrDigit(‘5’);
It returns true to the boolean variable k as ‘5’ is a digit.
boolean k =  Character.isLetterOrDigit(‘*’);
It returns false to the boolean variable k as ‘*’ is neither a letter nor a digit.

Character.isWhitespace()

This method can be used to check for an existing blank or gap in a String. It will return a boolean value true if the given argument is a white space and false otherwise.
boolean var_name = Character.isWhitespace(char_name);
Or, boolean var_name = Character.isWhitespace(character);
Example:
boolean k =  Character.isWhitespace(‘ ’);
It returns true to the boolean variable k as the given character is blank.
boolean k =  Character.isLetterOrDigit(‘*’);
It returns false to the boolean variable k as ‘*’ is not a blank.

Character.isUpperCase()

This method will return true if the given argument is an upper case letter, otherwise false.
Syntax:
boolean var_name = Character.isUpperCase(char_name);
Or, boolean var_name = Character.isUpperCase (character);
Example:
boolean k =  Character.isUpperCase (‘ B’);
It returns true to the variable k.
boolean k =  Character.isUpperCase (‘ x’);
It returns false to the variable k.

Character.isLowerCase()

This method will return true if the given argument is a lower case letter, otherwise false.
Syntax:
boolean var_name = Character.isLowerCase(char_name);
Or, boolean var_name = Character.isLowerCase (character);
Example:
boolean k =  Character.isLowerCase (‘ b’);
It returns true to the variable k.
boolean k =  Character.isLowerCase (‘ X’);
It returns false to the variable k.

Character.toUpperCase()

This method returns the argument in upper case character. The given character remains same if it is already in upper case. It returns the same character if the character used with the method is not a letter.
Syntax:
char var_name = Character.toUpperCase (char_name);
Or, char var_name = Character.toUpperCase(character);
Example:
char x = Character.toUpperCase(‘a’);
It returns A to the variable x.
char x = Character.toUpperCase(‘P’);
It returns P to the variable x.
char x = Character.toUpperCase(‘?’);
It will return the same character ‘?’ because the given character is not a letter.

Character.toLowerCase()

This method returns the argument in lower case character. The given character remains same if it is already in lower case. It returns the same character if the character used with the method is not a letter.
Syntax:
char var_name = Character.toLowerCase (char_name);
Or, char var_name = Character.toLowerCase (character);
Example:
char x = Character.toLowerCase (‘a’);
It returns a to the variable x.
char x = Character.toLowerCase (‘P’);
It returns p to the variable x.
char x = Character.toLowerCase (‘?’);
It will return the same character ‘?’ because the given character is not a letter.

 Program 01

Write a program to accept a character and check the case (upper/ lower) otherwise, check whether it is a digit or a special character.

 

 Program 02

Write a program to enter a letter. Display the new letter and ASCII code after changing the case.

Characters

ASCII Code
0-9

48-57

A-Z

65-90
a-z

97-122

 

 Program 03

Write a program to accept a string. Count and display the number of vowels present in the string.

 

<< Previous  Next>>

Leave a Reply

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